这是我的代码:
@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 ...
shapeLayer.removeFromSuperlayer()
你接着说:
... and stopping this basic animation ...
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
顺便说一下,当您浏览 CAShapeLayer
的文档时和 CABasicAnimation
, 不要忘记查看它们的父类(super class)的文档,即和 CALayer
和 CAAnimation
» CAPropertyAnimation
, 分别。底线是,当四处寻找有关某些特定类的属性或方法的文档时,您通常必须深入父类(super class)才能找到相关信息。
最后,Core Animation Programming Guide是很好的介绍,虽然它的示例是在 Objective-C 中,但所有概念都适用于 Swift。
关于swift - 如何从我的 UIView 中删除 CAShapeLayer 和 CABasicAnimation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49518958/