views:

600

answers:

2

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?

A: 

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

Ole Begemann
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.
Flocked
A: 
  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>

lawrence
Hi, it doesn't work, I tried this also yesterday. I wrote: `[UIView beginAnimations:nil context:nil];` `[UIView setAnimationTransition:110 forView:drum.view cache:YES];` `[self.view addSubview:drum.view];` `[UIView commitAnimations];`The drumview just appears, even if I set the animation duration to 10. As I set, it works, when I put this code to a IBAction or other methods, that doesn't have to do with the showing of a view. I tried a NSTimer in viewDidLoad and set the Timeintervall to 0 and the animation works, but just after 1 sec.
Flocked
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."
lawrence
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.
Flocked