3

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!

3 Answers 3

6

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).

2
  • Thanks, this answers the question of WHY it goes into a direct loop. I'll try using CAAnimation or just a base case for my loop as I did. Nov 12, 2013 at 22:38
  • Your guess is right, add NSLog to animation completion block, the log is out infinite.
    – Michael
    Jun 12, 2018 at 7:28
3

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.

2
  • This seemed like a great idea, however, I still have my 100% CPU.. Any idea why? Nov 12, 2013 at 14:37
  • 1
    That idea was for using self in block... But actually you are making recursive calls... animateView->animateView->animateView... which will cause stack over flow and also use CPU cycle... Recursion can only be use with base case... Base case means a case when you will not call method again... in your scenario you don't have any base case... and it goes in a loop. Nov 12, 2013 at 22:03
1

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){

                           }
                         ];
                     }
     ]; 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.