How to add an animation to the UIView in viewDidAppear?

11,678

I had the same problem and I think I found the solution on this SO question.

When viewDidAppear gets called you still don't see anything on the screen (despite the name), but you are about to. You can then use a performSelector:withDelay or an NSTimer to launch your animation. The delay can just be 0.1 and your animation will play just when the screen appears.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSLog(@"View did appear!");

    [self performSelector:@selector(animationCode) withObject:nil afterDelay:0.1f];
}

- (void)animationCode {
    // you animation code
}
Share:
11,678
Flocked
Author by

Flocked

Updated on June 25, 2022

Comments

  • Flocked
    Flocked almost 2 years

    I tried to add a animation to viewDidLoad and viewDidAppear, but it doesn't work:

    - (void)viewDidAppear:(BOOL)animated{
     [UIView beginAnimations:@"transition" context:NULL];
     [UIView setAnimationTransition:110 forView:self.view cache:YES];
     [UIView commitAnimations];
    }
    

    Why?