ios - CAShapeLayer 更改 lineWidth 导致崩溃

标签 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")
}

}

最佳答案

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

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

关于ios - CAShapeLayer 更改 lineWidth 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54353069/

相关文章:

ios - .calendarsForEntityType(EKEntityType.Reminder) 不返回提醒列表列表

ios - 检测 Sprite 之间的碰撞

swiftyjson - 调用可以抛出,但它被标记为 'try' 并且错误未被处理

c++ - OpenGL:仅使用 GL_POINTS 渲染一条线

Emacs hl-line : change color locally

indexing - awk - 打印包含初始分析中找到的最大值的所有行(行之间包含 U+2500 Unicode 字符)

ios - 如何调整 JSQ Messages CollectionViewCell 的气泡边距

ios - 尝试滑动删除当前单元格时,表格 View 单元格开始晃动

ios - SpriteKit 和 Swift 中的 isDynamic 和 contactDelegate

ios - UIImageView 在自定义 UICollectionViewCell 中返回 nil