如何更改 CAShapeLayer 内部的区域?
•浏览 1
How to change area inside CAShapeLayer?
我用自定义 UIBezierPath 和填充颜色创建了 CAShapeLayer。如何更改路径内的区域?让它更大/更低。
private var path = UIBezierPath()
private var shapeLayer = CAShapeLayer()
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch? {
let touchPoint = touch.location(in: self)
path.move(to: touchPoint)
}
}
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch? {
let touchPoint = touch.location(in: self)
path.addLine(to: touchPoint)
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = strokeColor.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = lineWidth
layer.addSublayer(shapeLayer)
}
}
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch? {
let touchPoint = touch.location(in: self)
path.addLine(to: touchPoint)
path.close()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.clear.cgColor
shapeLayer.fillColor = strokeColor.cgColor
shapeLayer.lineWidth = lineWidth
layer.addSublayer(shapeLayer)
}
}
所以我在更改区域时遇到了问题。如何更改填充颜色的区域?或者如何修改有/或没有某些点的路径?
需要的步骤:
- 用手势选择框架;
- 填色;
- 使用手势更新框架使其更大/更小(触摸开始/移动/结束)
类似于这个库 https://github.com/TinyCrayon/TinyCrayon-iOS-SDK
你不能改变 CAShapeLayer。
但是我已经通过 2 个步骤实现了这个效果。
首先 - 选择一个带有 bezierPath 的区域,保存路径。
第二步 - 创建 CGContext,使用 bezierPath 填充颜色的颜色在我们的图像上添加图像,然后使用我们的路径添加蒙版并添加功能擦除/取消擦除图像。