我使用自定义 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)
}
}
所以我在更改区域时遇到问题。如何改变填充颜色的区域?或者如何修改有/或没有某些点的路径?
所需步骤: - 用手势选择框架; - 填色; - 更新框架,通过手势使其变大/变小(触摸开始/移动/结束)
最佳答案
您无法更改 CAShapeLayer。
但是我通过两步就达到了这个效果。
首先 - 使用 bezierPath 选择一个区域,保存路径。
第二 - 创建 CGContext,使用 bezierPath 填充颜色的颜色在图像上添加图像,然后使用我们的路径添加蒙版并添加删除/取消删除图像功能。
关于swift - 如何更改CAShapeLayer内的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56970355/