CAShapeLayer更改lineWidth会导致崩溃

时间:2019-01-24 18:18:35

标签: ios swift line cashapelayer

所以我有一个不错的子类,可以在某个时候调整其大小,然后我调用updateFrame() 将lineWidth更改为任何值都会导致崩溃。我需要的是能够立即更改lineWidth而无需动画。

https://cdn1.imggmi.com/uploads/2019/1/24/a93765689e524d45116f942ce734275e-full.png

https://cdn1.imggmi.com/uploads/2019/1/24/12a880d41cff1f69ef6bd2f974033627-full.png

class OvalLayer: CAShapeLayer {

func updateFrame(_ with: CGRect) {
    let newFrame = with.insetBy(dx: frame.size.width * 0.07, dy: frame.size.height * 0.07)
    path = UIBezierPath(ovalIn: newFrame).cgPath
    lineWidth = (min(frame.size.width, frame.size.height) / 50) //if i remove this line it will be ok
}

init(frame: CGRect) {
    super.init()
    self.frame = frame
    let newFrame = frame.insetBy(dx: frame.size.width * 0.07, dy: frame.size.height * 0.07)
    lineWidth = (min(frame.size.width, frame.size.height) / 50)
    strokeColor = UIColor.white.cgColor
    fillColor = UIColor.clear.cgColor
    path = UIBezierPath(ovalIn: newFrame).cgPath
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

1 个答案:

答案 0 :(得分:0)

是的,一分钟后,我写了一个解决方案。我认为,当您更新图层属性时,它会自行重新初始化(只是我的猜测)

override init(layer: Any) {
    super.init(layer: layer)
}