[英]changing size of CALayer also changes size of previously added layer's size
i'm trying to draw over UIImageView with this custome class and i would like to be able to change the size and color of lines being drawn by user.我正在尝试使用此自定义 class 绘制 UIImageView 并且我希望能够更改用户绘制的线条的大小和颜色。 But every time i change the size or color of CALayer Previously added layer's color and size also changes then.
但是每次我更改 CALayer 的大小或颜色时,先前添加的图层的颜色和大小也会随之改变。
class DrawingImageView: UIImageView {
private lazy var path = UIBezierPath()
private lazy var previousTouchPoint = CGPoint.zero
var strokeColor: CGColor!
var strokeWidth: CGFloat!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let shapeLayer = CAShapeLayer()
shapeLayer.lineWidth = strokeWidth ?? 4
shapeLayer.strokeColor = strokeColor ?? UIColor.black.cgColor
shapeLayer.lineCap = .round
shapeLayer.lineJoin = .round
layer.addSublayer(shapeLayer)
if let location = touches.first?.location(in: self) { previousTouchPoint = location }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
if let location = touches.first?.location(in: self) {
path.move(to: location)
path.addLine(to: previousTouchPoint)
previousTouchPoint = location
let layerA = layer.sublayers?.last as! CAShapeLayer
layerA.path = path.cgPath
}
}
}
this is how i assign that class to tempImageView这就是我将 class 分配给 tempImageView 的方式
let mainImageView = UIImageView()
var tempImgView: DrawingImageView?
mainImageView.frame = CGRect(x: 0, y: self.view.bounds.height / 7, width: imageWidth, height: imageHieght)
mainImageView.contentMode = .scaleAspectFit
mainImageView.layer.zPosition = 2
mainImageView.isOpaque = false
mainImageView.backgroundColor = .clear
mainImageView.isUserInteractionEnabled = true
mainImageView.tintColor = .clear
self.view.addSubview(mainImageView)
func DrawOverImage() {
mainImageView.isHidden = true
let drawnImageView = DrawingImageView(frame: mainImageView.frame)
drawnImageView.image = mainImageView.image
drawnImageView.contentMode = .scaleAspectFit
drawnImageView.isUserInteractionEnabled = true
drawnImageView.layer.zPosition = 3
self.tempImgView?.isUserInteractionEnabled = true
self.tempImgView = drawnImageView
self.view.addSubview(tempImgView!)
}
i also tried creating array of CAShapeLayers and add layers to an array and then assign replace subLayer like View.layer.subLayers[0] = layers[0] but that also didn't work and i couldn't find any useful resource online.我还尝试创建 CAShapeLayers 数组并将图层添加到数组中,然后分配替换 subLayer,如 View.layer.subLayers[0] = layers[0] 但这也不起作用,我在网上找不到任何有用的资源。
any help or suggestion would be really appreciated, thanks.任何帮助或建议将不胜感激,谢谢。
That is because you are continuing the same path each time and so the latest stroke color and width gets applied to the whole path.那是因为您每次都在继续相同的路径,因此最新的笔触颜色和宽度将应用于整个路径。
Without looking at much of your other logic, I believe you should initialize a new path each time in your touchesBegan
and this new path should be used in touchesMoved
在不看其他逻辑的情况下,我相信您每次都应该在
touchesBegan
中初始化一条新路径,并且应该在touchesMoved
中使用这条新路径
So in touchesBegan
, try adding path = UIBezierPath()
after layer.addSublayer(shapeLayer)
and see if this gives you what you are looking for所以在
touchesBegan
中,尝试在layer.addSublayer(shapeLayer)
path = UIBezierPath()
) ,看看这是否给了你想要的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.