user1028028
user1028028

Reputation: 6433

UIVIew animate with duration hogs CPU and RAM

The following method animates the transparency of a view that is between two other views. Theproblem with it is : 1) It never stops 2) CPU:107% and constant memory increase (until app crashes)

How do I get It to stop animating and while it is animating not use 100%CPU and memory increase to 1GB in one minute:

-(void)animateFade
{
    if(!self.canRestartFade){
        return;
    }
    self.canRestartFade = NO;

    [UIView animateWithDuration: 2
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         self.avatarLogoHaloImageView.alpha = 0.0;
                     }
                     completion:^(BOOL finished){
                         // Wait one second and then fade in the view
                         [UIView animateWithDuration:2
                                               delay: 1.0
                                             options:UIViewAnimationOptionCurveEaseOut
                                          animations:^{
                                              self.avatarLogoHaloImageView.alpha = 1.0;
                                          }
                                          completion:^(BOOL finished){
                                              self.canRestartFade = YES;
                                              [self animateFade];
                                          }];
                     }];
}

Upvotes: 0

Views: 302

Answers (2)

Legoless
Legoless

Reputation: 11112

I am assuming that animateFade is this function, so since you set canRestartFade property to YES here, and you are checking if it's set to NO, then this kind of creates infinite loop, which could also be the reason for your 100% CPU usage.

Upvotes: 0

Fogmeister
Fogmeister

Reputation: 77661

If it goes on forever then a better way to do it would be like this...

[UIView animateWithDuration: 2
                      delay: 0.0
                    options: UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoReverse | UIViewAnimationOptionRepeat
                 animations:^{
                     self.avatarLogoHaloImageView.alpha = 0.0;
                 }
                 completion:nil];

This should repeat the animation forever and fade out then in etc...

Also, because it is a single function you don't get your recursive calls which will be adding to the call stack each time... or something...

Anyway, give this a go.

No need to run it again and again. Just run this line once.

Upvotes: 1

Related Questions