I am using CABasicAnimation to rotate a UIImageView and I cannot resume paused animation. Animation starts with the viewDidLoad method:
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"];
I need to have a continuous repeating animation that resumes when the view appears on the screen. So I read this post ( here ) and implemented the solution by providing my apple ( solution ) to stop and resume the layer animation. So, I used these methods:
-(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; }
And I added these methods to view WillAppear and viewWillDisappear using and passing a layer of the myImage property:
-(void)viewWillDisappear:(BOOL)animated{ [self pauseLayer:self.myImage.layer]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self resumeLayer:self.myImage.layer]; }
The image has initial rotation when the image is displayed on the screen. But then I put the UIViewController on the screen, and then return to the image rotation view. The problem is that the rotation of the image does not resume when the image appears on the screen. I checked: my image's UIImageView property is non-zero and the viewWillDisappear and viewWillAppear methods are called. But the animation does not resume. Am I something wrong or missed something?
source share