如何掩盖和删除Cashapelayer?
我正在尝试使擦除功能与CashApelayer合作。当前代码:
class MaskTestVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let image = UIImage(named: "test.jpg")!
let testLayer = CAShapeLayer()
testLayer.contents = image.cgImage
testLayer.frame = view.bounds
let maskLayer = CAShapeLayer()
maskLayer.opacity = 1.0
maskLayer.lineWidth = 20.0
maskLayer.strokeColor = UIColor.black.cgColor
maskLayer.fillColor = UIColor.clear.cgColor
maskLayer.frame = view.bounds
testLayer.mask = maskLayer
view.layer.addSublayer(testLayer)
}
}
我在想如果我创建掩码层,那么我可以在掩码层上绘制一条路径,并删除图像的一部分:
let path = UIBezierPath()
path.move(to: CGPoint(x: 100.0, y: 100.0))
path.addLine(to: CGPoint(x: 200.0, y: 200.0))
maskLayer.path = path.cgPath
但是,似乎当我添加MaskLayer
时,它涵盖了整个图像,我看不到下面的内容。我在这里做错了什么?
I am trying to get erase functionality working with CAShapeLayer. Current code:
class MaskTestVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let image = UIImage(named: "test.jpg")!
let testLayer = CAShapeLayer()
testLayer.contents = image.cgImage
testLayer.frame = view.bounds
let maskLayer = CAShapeLayer()
maskLayer.opacity = 1.0
maskLayer.lineWidth = 20.0
maskLayer.strokeColor = UIColor.black.cgColor
maskLayer.fillColor = UIColor.clear.cgColor
maskLayer.frame = view.bounds
testLayer.mask = maskLayer
view.layer.addSublayer(testLayer)
}
}
I was thinking if I create a mask layer then I could draw a path on the mask layer and erase parts of the image eg:
let path = UIBezierPath()
path.move(to: CGPoint(x: 100.0, y: 100.0))
path.addLine(to: CGPoint(x: 200.0, y: 200.0))
maskLayer.path = path.cgPath
However it seems that when I add the maskLayer
it covers the entire image and I can't see the contents below it. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过创建自定义
calayer
subridingdraw(在CTX:CGCONTEXT)
:这是一个完整的示例...我们将创建一个
uiview
子类,并且由于效果将“刮擦图像”,我们将其称为scratchOffimageView
:,示例视图控制器:
和 看起来像这样开始 - 我使用了3:2映像,然后在带有红色文本的浅灰色标签上叠加,以便我们可以看到我们正在“刮擦”图像:
经过一点“刮擦”之后:
以及在很多“划痕”之后:
You can use a bezier path mask to "erase" the path by creating a custom
CALayer
subclass and overridingdraw(in ctx: CGContext)
:Here's a complete example... we'll create a
UIView
subclass and, since the effect will be "scratching off the image" we'll call itScratchOffImageView
:and, an example view controller:
It will look like this to start - I used a 3:2 image, and overlaid it on a light-gray label with red text so we can see that we are "scratching off" the image:
then, after a little bit of "scratching":
and after a lot of "scratching":
Masklayer 没有初始路径,因此其内容没有填充,也可以将其填充。CLEAR将完全掩盖图像。
这对我有用:
实际上不确定中风和填充的组合是否有效,但也许其他人提出了解决方案。如果您想从路径上切出形状,则可以尝试这样的事情:
maskLayer has no initial path therefore its content is not filled, also filling it with .clear will completely mask the image.
This works for me:
Not actually sure if a combination of stroke and fill works but maybe someone else comes up with a solution. If you want to cut out shapes from your path you could try something like this: