Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get solution for stopping/resuming CABasicAnimation working?
    primarykey
    data
    text
    <p>I'm using CABasicAnimation for rotating UIImageView and I'm unable to resume paused animation. The animation starts in viewDidLoad method:</p> <pre><code>UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]]; self.myImage = img; [self.view addSubview:img]; [img release]; CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; fullRotation.fromValue = [NSNumber numberWithFloat:0]; fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)]; fullRotation.duration = 10; fullRotation.repeatCount = LARGE_VAL; [img.layer addAnimation:fullRotation forKey:@"360"]; </code></pre> <p>I need to have non-stop repeatable animation that resumes when the view appears on screen. So I have read this post (<a href="https://stackoverflow.com/questions/2306870/is-there-a-way-to-pause-a-cabasicanimation">here</a>) and implemented solution provided my apple (<a href="http://developer.apple.com/library/ios/#qa/qa1673/_index.html" rel="nofollow noreferrer">solution</a>) for stopping and resuming layer animation. So, I have used these methods:</p> <pre><code>-(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; } </code></pre> <p>And I have added these methods to viewWillAppear and viewWillDisappear using and passing the layer of myImage property:</p> <pre><code>-(void)viewWillDisappear:(BOOL)animated{ [self pauseLayer:self.myImage.layer]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self resumeLayer:self.myImage.layer]; } </code></pre> <p>The image does initial rotation when the view goes on screen. But then I'm putting some UIViewController on screen, then going back to the view with image rotation. The problem is the rotation of the image is not resumed when the view appears on screen. I have checked: my image UIImageView property is not nil, both viewWillDisappear and viewWillAppear methods get invoked. But the animation is not resuming. Did I something wrong, or missed something? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload