我似乎无法从strokeEnd
CAShapeLayer的属性中删除动画。
文档说该属性是animatable
默认设置的,但不是默认设置的,我无法查明问题所在。有什么建议在哪里看?
这是我的代码:
class ViewController: UIViewController {
let circle = CAShapeLayer()
override func viewDidLoad() {
super.viewDidLoad()
// Circle
circle.fillColor = UIColor.clearColor().CGColor
circle.strokeColor = UIColor.blackColor().CGColor
circle.lineWidth = 10
circle.strokeEnd = 0
circle.lineJoin = kCALineJoinRound
circle.path = UIBezierPath(ovalInRect: CGRectMake(60, 140, 200, 200)).CGPath
circle.actions = ["strokeEnd" : NSNull()]
// Show Button
let showButton = UIButton(frame: CGRectMake(40, 40, 240, 40))
showButton.addTarget(self, action: "showButton", forControlEvents: UIControlEvents.TouchUpInside)
showButton.setTitle("Show circle", forState: UIControlState.Normal)
showButton.backgroundColor = UIColor.greenColor()
// Add to view
self.view.layer.insertSublayer(circle, atIndex: 1)
self.view.addSubview(showButton)
}
func showButton() {
circle.strokeEnd = 1
}
}
您描述的将图层的strokeEnd动作设置为NSNull的方法有效,但这有点像大锤。当您这样做时,您将永远杀死图层的strokeEnd属性的隐式动画。
如果那是您想要的,那没关系。但是,我倾向于使用DavidRönnqvist在您链接的答案中列出的第二种方法:在CATransaction的begin / commit块内更改图层。这是David的答案的代码(这很出色,就像他的帖子一样)。
[CATransaction begin];
[CATransaction setDisableActions:YES];
// change your property here
yourShapeLayer.strokeEnd = 0.7;
[CATransaction commit]; // animations are disabled until here...
该代码在Objective-C中。将其翻译为Swift还不错:
CATransaction.begin()
CATransaction.setDisableActions(true)
yourShapeLayer.strokeEnd = 0.7
CATransaction.commit()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句