Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animations not restarting when moving between view controllers

I have a number of animations that start automatically when my root view controller is loaded. These animations are included in viewDidLoad. When i navigate to my next view controller and return to the root view controller, all the animations have stopped. The same behaviour occurs when i press the "home" button and then return to the the view.

I'm sure i'm missing something very basic here but would appreciate any help. Thanks.

EDIT 1:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self beeBobbing];
    //animate the butterfly oscillating
    [self butterflyOscillate];

}

-(void) beeBobbing
{
    [UIView animateWithDuration:1.0f delay:0 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction ) animations:^{
        CGPoint bottomPoint = CGPointMake(215.0, 380.0);
        imgBee.center = bottomPoint;
    } completion:^(BOOL finished) {

    }];

}

EDIT 2: This type of animation seems to restart when changing between views:

-(void) animateClouds
{
    [UIView animateWithDuration:30.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGPoint leftScreenCenter = CGPointMake(-420.0, 119.0);
        imgClouds.center = leftScreenCenter;
    } completion:^(BOOL finished) {
        CGPoint rightScreenCenter = CGPointMake(1450.0, 119.0);
        imgClouds.center = rightScreenCenter;
        [UIView animateWithDuration:40.0f delay:0 options: (UIViewAnimationOptionCurveLinear | UIViewAnimationOptionRepeat) animations:^{
            CGPoint leftScreenCenter = CGPointMake(-420.0, 119.0);
            imgClouds.center = leftScreenCenter;
        } completion:^(BOOL finished) {

        }];

    }];

}
like image 670
garethdn Avatar asked May 18 '12 14:05

garethdn


3 Answers

You need to add the animations in viewWillAppear:. viewDidLoad is called once when the controller is initialized and the view is loaded either from a nib or created in the -loadView method (if overridden) and won't be called again until the controller is destroyed and created again. The methods that fire when you navigate back and forward are viewWillAppear: viewDidAppear viewWillDisappear:.

like image 23
graver Avatar answered Oct 26 '22 17:10

graver


i think if you give all animation in viewWillAppear: then its work fine... thats it... :)

like image 66
Paras Joshi Avatar answered Oct 26 '22 19:10

Paras Joshi


viewDidLoad is just called when you first load your controller the first time, if then you navigate to other viewControllers with some push or you change tab, you don't pass in viewDidLoad...

try to start your animations in the method - (void)viewWillAppear:(BOOL)animated

like image 36
meronix Avatar answered Oct 26 '22 19:10

meronix