微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 如何从我的UIView中删除CAShapeLayer和CABasicAnimation?

这是我的代码

@objc func drawForm() {
    i = Int(arc4random_uniform(UInt32(formNames.count)))
    var drawPath = actualFormNamesFromFormClass[i]
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.black.cgColor
    shapeLayer.linewidth = 6
    shapeLayer.frame = CGRect(x: -115,y: 280,width: 350,height: 350)

    var paths: [UIBezierPath] = drawPath()
    let shapeBounds = shapeLayer.bounds
    let mirror = CGAffineTransform(scaleX: 1,y: -1)
    let translate = CGAffineTransform(translationX: 0,y: shapeBounds.size.height)
    let concatenated = mirror.concatenating(translate)

    for path in paths {
        path.apply(concatenated)
    }

    guard let path = paths.first else {
        return
    }

    paths.dropFirst()
        .forEach {
            path.append($0)
    }

    shapeLayer.transform = CATransform3DMakeScale(0.6,0.6,0)
    shapeLayer.path = path.cgPath

    self.view.layer.addSublayer(shapeLayer)

    strokeEndAnimation.duration = 30.0
    strokeEndAnimation.fromValue = 0.0
    strokeEndAnimation.tovalue = 1.0
    shapeLayer.add(strokeEndAnimation,forKey: nil)
}

这段代码为shapeLayer路径的绘图设置了动画,但我无法在网上找到任何关于删除此图层并停止此基本动画或删除被绘制的cgPath的内容…任何帮助将不胜感激!

解决方法

你说:

I can’t find anything online about removing this layer …

这是removeFromSuperlayer().

shapeLayer.removeFromSuperlayer()

你继续说:

… and stopping this basic animation …

这是removeAllAnimations

shapeLayer.removeAllAnimations()

注意,这会立即将strokeEnd(或您动画的任何属性)更改回其先前的值.如果你想在你停止它的地方“冻结”它,你必须抓住presentation图层(它捕捉图层的属性,因为它们是动画中期),保存适当的属性,然后更新你的图层的属性正在停止动画:

if let strokeEnd = shapeLayer.presentation()?.strokeEnd {
    shapeLayer.removeAllAnimations()
    shapeLayer.strokeEnd = strokeEnd
}

最后,你继续说:

… or removing the cgPath that gets drawn.

只需将其设置为nil:

shapeLayer.path = nil

顺便说一句,当您浏览CAShapeLayerCABasicAnimation的文档时,请不要忘记查看其超类的文档,分别是CALayerCAAnimation»CAPropertyAnimation.最重要的是,当四处寻找有关特定类的属性方法的文档时,您通常需要深入研究超类以查找相关信息.

最后,Core Animation Programming Guide是很好的介绍,虽然它的例子在Objective-C中,但所有概念都适用于Swift.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐