如何更改 CAShapeLayer 内的区域?
How to change area inside CAShapeLayer?
我创建了带有自定义 UIBezierPath 和填充颜色的 CAShapeLayer。如何更改路径内的区域?使其成为 bigger/lower.
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)
}
}
所以我在更改区域时遇到问题。如何更改填充颜色的区域?或者如何修改没有点的路径with/or?
需要的步骤:
- select 带手势的框架;
- 填色;
- 更新框架使其 bigger/smaller 带有手势(触摸 began/moved/ended)
您不能更改 CAShapeLayer。
但是我分两步就达到了这个效果
首先-select一个有bezierPath的区域,保存路径。
其次 - 创建 CGContext,使用 bezierPath 填充颜色在我们的图像上添加图像,然后使用我们的路径添加遮罩并添加功能 erase/unerase 图像。
我创建了带有自定义 UIBezierPath 和填充颜色的 CAShapeLayer。如何更改路径内的区域?使其成为 bigger/lower.
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)
}
}
所以我在更改区域时遇到问题。如何更改填充颜色的区域?或者如何修改没有点的路径with/or?
需要的步骤: - select 带手势的框架; - 填色; - 更新框架使其 bigger/smaller 带有手势(触摸 began/moved/ended)
您不能更改 CAShapeLayer。
但是我分两步就达到了这个效果
首先-select一个有bezierPath的区域,保存路径。
其次 - 创建 CGContext,使用 bezierPath 填充颜色在我们的图像上添加图像,然后使用我们的路径添加遮罩并添加功能 erase/unerase 图像。