Is there a way to pause CABasicAnimation?

I have basic animation for the iPhone. Is there a way that I can β€œpause” the animation so that the position of the view is maintained? I suppose one way to do this would be to make the animation "fill" instead of calling "delete" on it, how would I do it?

CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2]; rotationAnimation.duration = 100; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = HUGE_VALF; rotationAnimation.removedOnCompletion = NO; rotationAnimation.fillMode = kCAFillModeForwards; [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
+45
iphone core-animation cabasicanimation
Feb 21 '10 at 18:20
source share
5 answers

The recent Apple Tech Note QA1673 describes how to pause / resume layer animations.

A list of pausing and resuming animations is shown below:

 -(void)pauseLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; layer.speed = 0.0; layer.timeOffset = pausedTime; } -(void)resumeLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer timeOffset]; layer.speed = 1.0; layer.timeOffset = 0.0; layer.beginTime = 0.0; CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; layer.beginTime = timeSincePause; } 

Edit: iOS 10 introduced a new API - UIViewPropertyAnimator, which allows you to process animations more interactively, for example, makes it easy to pause and resume animations, or β€œsearch” it for a specific progress value.

+150
Jun 09 2018-10-06T00:
source share

Answer for Swift 3:

Credits @ Vladimir

The code:

  func pauseAnimation(){ let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) layer.speed = 0.0 layer.timeOffset = pausedTime } func resumeAnimation(){ let pausedTime = layer.timeOffset layer.speed = 1.0 layer.timeOffset = 0.0 layer.beginTime = 0.0 let timeSincePause = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime layer.beginTime = timeSincePause } 
+12
Nov 03 '16 at 11:20
source share

Set the current state of the layer of your view to match the state of presentationLayer , and then remove the animation:

 CALayer *pLayer = [myView.layer presentationLayer]; myView.layer.transform = pLayer.transform; [myView.layer removeAnimationForKey:@"rotationAnimation"]; 
+6
Feb 23 '10 at 1:55
source share

You can use a timer or handle the animation delegation method:

 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 

Here is my code:

 // ... [self startAnimation]; // ... - (void)startAnimation { CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.fromValue = [NSNumber numberWithFloat:0]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_2_PI]; rotationAnimation.duration = 1.0; rotationAnimation.cumulative = YES; // rotationAnimation.repeatCount = 0; // <- if repeatCount set to infinite, we'll not receive the animationDidStop notification when the animation is repeating rotationAnimation.removedOnCompletion = NO; rotationAnimation.fillMode = kCAFillModeForwards; rotationAnimation.delegate = self; // <- hanlde the animationDidStop method [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { if (shouldContinueAnimation) // <- set a flag to start/stop the animation [self startAnimation]; } 

Hope this helps you.

0
Dec 12 2018-11-12T00
source share

Simplest

 self.viewBall.layer.position = self.viewBall.layer.presentationLayer().position 
-one
May 29 '15 at 11:46
source share



All Articles