How to get a solution to stop / resume CABasicAnimation?

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?

+6
source share
3 answers

Workarounds, workarounds and workarounds. So far, the solution I got is to discard the proposed solution for stopping / resuming layer animations :) to remove all animations in viewWillDisappear:

 [self.myImage.layer removeAllAnimations]; 

and then start a new animation in viewWillAppear. I know that I do not renew the old one, but in my case it is not so noticeable to the human eye, so I think that I will be fine. But if anyone has a solution provided by an apple, it should work.

+6
source

I had the same problem with yours. After multiholding ... I have a solution for this.

In your case, just add the following code when installing CABasicAnimation :

 fullRotation.removedOnCompletion = NO; 

What is it. Hope this works for you.

+32
source

I think your problem is memory management.

Try this instead:

 UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]]; self.myImage = 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; [self.myImage.layer addAnimation:fullRotation forKey:@"360"]; [self.view addSubview:self.myImage]; 

Tell me if this works.

0
source

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


All Articles