我可以在没有动画的情况下更改 strokeEnd 属性 吗?
Can I change a strokeEnd property without animation?
我似乎无法从我的 CAShapeLayer 的 strokeEnd
属性 中删除动画。
文档说 属性 是 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
}
}
我找到问题了。这解决了它:
circle.actions = ["strokeEnd" : NSNull()]
可以在此处找到更多信息:Change CAShapeLayer without Animation
您描述的将图层的 strokeEnd 动作设置为 NSNull 的方法可行,但它有点像大锤。当你这样做时,你会永远杀死图层的 strokeEnd 属性 的隐式动画。
如果这就是您想要的,那没关系。但是,我倾向于使用 David Rönnqvist 在您链接的答案中列出的第二种方法:Making your layer change inside a CATransaction begin/commit block。这是 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()
我似乎无法从我的 CAShapeLayer 的 strokeEnd
属性 中删除动画。
文档说 属性 是 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
}
}
我找到问题了。这解决了它:
circle.actions = ["strokeEnd" : NSNull()]
可以在此处找到更多信息:Change CAShapeLayer without Animation
您描述的将图层的 strokeEnd 动作设置为 NSNull 的方法可行,但它有点像大锤。当你这样做时,你会永远杀死图层的 strokeEnd 属性 的隐式动画。
如果这就是您想要的,那没关系。但是,我倾向于使用 David Rönnqvist 在您链接的答案中列出的第二种方法:Making your layer change inside a CATransaction begin/commit block。这是 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()