我用自定义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)
}
}
所以我在更改区域时遇到问题。如何更改填充颜色的区域?或如何在有/没有点的情况下修改路径?
所需步骤: -选择带有手势的帧; - 填色; -更新框架使手势变大/变小(触摸开始/移动/结束)
答案 0 :(得分:2)
您不能更改CAShapeLayer。
但是我已经通过两个步骤实现了这种效果。
首先-使用bezierPath选择一个区域,保存路径。
第二个-创建CGContext,使用bezierPath填充颜色将图像添加到我们的图像上,然后使用路径添加蒙版并添加擦除/取消擦除图像功能。