Search code examples
iosperformselector

Switch to different view after a set of animations are completed


I have a view with a set of UIButtons. When one button is pressed, I want the rest to translate off the screen, then change the viewcontroller to a new view. My two methods to do this are:

-(IBAction)button1 {
   [UIView beginAnimations: nil context: nil];
   [UIVeiw setAnimationDuration:0.5];
   [UIView setAnimationCurve:UIVewAnimationCurveEaseIn];
   button2.transform = CGAffineTransforMakeTranslation(0, 520);  
   [UIView commitAnimations];
   [self performSelector:@selector(switchtoview2) withObject:self afterdelay:0.6];
}

-(void)switchtoview2 {
   View2ViewController *view2 = [[View2ViewController alloc] initWithNibName:nil bundle: nil];
   view2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   [self presentModalViewController:view2 animated:YES];
}

But when I press the first button, it simply breaks without leaving anything to go from in the debugger. How can I fix this, or at least detect what's gone wrong?


Solution

  • You should use the newer block methods. They have built-in completion blocks that will be executed once the animation completes.

    EDIT: For your situation, use the one where you can give options to use that animationCurve:

        [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         button2.transform = CGAffineTransforMakeTranslation(0, 520);
                     } completion:^(BOOL finished) {
                         [self switchtoview2];
                     }];