Question

Hi everyone, small question.

I have an UIView class that I created, AnimatedUIView. In the init, I create a subview that I animate like this:

   - (void)animateView{
    [UIView animateWithDuration:4.0
                     delay:1.0
                     options: nil
                     animations:^{ 
                         // Animation PART 1
                         } 
                     completion:^(BOOL  completed){
                       [UIView animateWithDuration:4.0
                           animations:^{ 
                               // Animation PART 2
                               } 
                           completion:^(BOOL  completed){
                             [self animateView];
                           }
                         ];
                     }
     ]; 

}

I have to call [self animateView] myself in the second completion block rather than using option repeat because I want to delay 1 second only the first animation.

Everything works fine, I alloc and init my view and add it as a subview, and it is animated as should.

But, when I close my superview, I guess ARC does its work and deallocs the animatedView, but my CPU goes 100 % ! I have investigated and if I comment the call to [self animateView], my CPU doesn't skyrocket when I close the superview.

So I've managed to solve the problem (by putting a condition before the call and changing a boolean value before closing the superview, keepAnimating = NO) , I just wanted to understand WHY it does this? Why does it try to keep animating, and why does it use so much CPU? If I put an NSLog in the completion block, I first see it every 8 seconds, but when I close the superview, the NSLog just keeps appearing every ms...

BTW: it relates to this question : UIView animation using 90%CPU , which was not really answered. Thanks a lot!

Was it helpful?

Solution

CPU going to 100% almost always means infinite recursion.

We can only guess because only Apple knows what's inside the animation source code.

In this case I guess this could be caused by triggering the animation when the animation cannot run anymore (e.g. there is no layer since we are in dealloc state). That would mean the completion handler is called immediately and that leads to infinite recursion.

There are safer ways to create infinite animations (e.g. using CAAnimation on the layer).

OTHER TIPS

You should not use self in side the block. It will create a retain cycle. If it is necessary to call self create a weak self pointer. Code like this

__weak typeof(self) weakself = self;
[UIView animateWithDuration:4.0
                     delay:1.0
                     options: nil
                     animations:^{ 
                         // Animation PART 1
                         } 
                     completion:^(BOOL  completed){
                       [UIView animateWithDuration:4.0
                           animations:^{ 
                               // Animation PART 2
                               } 
                           completion:^(BOOL  completed){
                             [weakself animateView];
                           }
                         ];
                     }
     ];

But main issue with your code is you calling animateView recursively without any base case. So i consuming CPU cycles... do not use animateView without base case.

Try using timer

Write following line in your viewDidLoad or any where else from where you want animation to start.

[NSTimer scheduledTimerWithTimeInterval:9 target:self selector:@selector(animateView) userInfo:nil repeats:YES];
// 9 is total animation duration.....

update your animateView method as follows:

- (void)animateView{
    [UIView animateWithDuration:4.0
                     delay:1.0
                     options: nil
                     animations:^{ 
                         // Animation PART 1
                         } 
                     completion:^(BOOL  completed){
                       [UIView animateWithDuration:4.0
                           animations:^{ 
                               // Animation PART 2
                               } 
                           completion:^(BOOL  completed){

                           }
                         ];
                     }
     ]; 

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top