How to add an animation to the UIView in viewDidAppear?
Asked Answered
D

3

7

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?

Declivous answered 2/2, 2010 at 23:52 Comment(0)
L
24

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
}
Lemcke answered 5/2, 2011 at 16:58 Comment(1)
I read a lot of articles before. And only this happend me, Thanks.Zosima
Q
0

You are not telling the view which state it should animate to so it won't do anything. You need to place code between beginAnimations:context: and commitAnimations that changes the appearance of the view (e.g. by removing one subview and adding another).

Quid answered 2/2, 2010 at 23:57 Comment(1)
Hm, I think I dont need to tell him this. I tried my code into a IBAction and it works. I wrote setAnimationTransition: forView: so I wrote in that the animation should be in the own view. It just doesn't work in the viewDidLoad and viewDidAppear-methode.Declivous
I
0
  1. You're not using beginAnimations: and commitAnimations correctly. You're supposed to put something in between them that normally wouldn't be animated: e.g. with self.view.alpha = 0.5 you get a fading effect. They have no effect on anything that isn't between them.

  2. By the time viewDidAppear: is called, your view, well... has appeared. It's too late to animate anything. What you actually want to do is something like this:

    - (void)showMyViewWithAnimation {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationTransition:110 forView:childView cache:YES];
        [parentView addSubview:childView];
        [UIView commitAnimations];
    }
    

    In the example above, childView is what in your example is called self.view.

  3. Please write out the name of the transition; no one knows what 110 is by looking at it. It's bad style. </pedantry>

Ibidem answered 3/2, 2010 at 9:1 Comment(2)
The bug in that code is that you should be doing setAnimationTransition:forView: on self.view, not drum.view. In the docs, it says: "Set the transition on the container view."Ibidem
But i want the effect for the drum.view ;) Thats the reason, why I tried to get the animation into the viewDidLoad of the drumViewController.Declivous

© 2022 - 2024 — McMap. All rights reserved.