Question

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?

Was it helpful?

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];
                 }];

OTHER TIPS

Since iOS 4, you should be using the newer block based UIView animation, which what do you know, has built in completion blocks! They're much easier to use as well considering Xcode's code completion will generate most of this for you. Just add the code you want to happen upon animation completion to the completion block at the bottom.

[UIView animateWithDuration:2.0 animations:^{
    button2.transform = CGAffineTransforMakeTranslation(0, 520);  
} completion:^(BOOL finished) {

    View2ViewController *view2 = [[View2ViewController alloc] initWithNibName:nil bundle: nil];
    view2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:view2 animated:YES];
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top