Can I change the strokeEnd property without animation?

I cannot remove the animation from the strokeEnd property of my CAShapeLayer.

The documentation states that the property is animatable but not animated by default, and I cannot pinpoint the problem. Any suggestions where to look?

Here is my code:

 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 } } 

CAShapeLayer strokeEnd animation

+6
source share
2 answers

The approach that you describe for setting the layerInd action of a layer to NSNull works, but it's a bit of a sledgehammer. When you do this, you will forever lose the implicit animation of the strokeEnd property of your layer.

If that's what you want, that's fine. However, I prefer to use the second approach that David Ronquist lists in the answer you linked: changing your layer inside the CATransaction trigger / lock block. Here is the code for the answer from David (which is great, since his messages are always there).

 [CATransaction begin]; [CATransaction setDisableActions:YES]; // change your property here yourShapeLayer.strokeEnd = 0.7; [CATransaction commit]; // animations are disabled until here... 

This code is in Objective-C. Translating it to Swift is not so bad:

 CATransaction.begin() CATransaction.setDisableActions(true) yourShapeLayer.strokeEnd = 0.7 CATransaction.commit() 
+14
source

I found a problem. This solves:

 circle.actions = ["strokeEnd" : NSNull()] 

More information can be found here: Change CAShapeLayer without animation

+2
source

Source: https://habr.com/ru/post/986917/


All Articles