ICL1901
ICL1901

Reputation: 7778

iOS - How do I perform two transitions sequentially?

I have taken some code from other SO questions, but I must be doing something wonky. The app jumps from the first view to the third. What I'm trying to achieve is:

ViewController 1 Image 1 - loads with an image. Quickly cross-disolves into ViewController 1 image 2.

ViewController 1 Image 2 - flips to ViewController 2.

The cross-disolve happens but takes me to VC2. I've battered at this for most of the day. It's time to ask for help while I go sit in a bathtub.

Here's what I've been doing:

    - (void)viewDidLoad

        {
            NSLog(@"%s", __FUNCTION__);
            [super viewDidLoad];
        }

        - (void)viewDidAppear:(BOOL)animated {
            NSLog(@"%s", __FUNCTION__);

            sleep (2);
            [self transition1]; //showing image 1
        }

        - (void) transition1 {
            NSLog(@"%s", __FUNCTION__);
            /*
            [UIView transitionFromView:firstView
                          toView:secondView
                        duration:3.0
                         options:UIViewAnimationOptionTransitionCrossDissolve
                        completion:^(BOOL finished) {
                            [firstView removeFromSuperview];
                        }];
             */

            //this transition doesn't happen

UIImage * secondImage = [UIImage imageNamed:@"image2.png"];

[UIView transitionWithView:self.firstView
                        duration:5.0f
                         options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            self.imageView.image = secondImage;
                        } completion:NULL];



            sleep (2);
            [self transition2];
        }

        - (void) transition2 {
            NSLog(@"%s", __FUNCTION__);
            self.patterns = [[PatternViewController alloc] initWithNibName:@"PatternView_iPad" bundle:nil];
            self.patterns.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentViewController:patterns animated:YES completion:nil];
        }

Thanks for any help.

UPDATE

I've updated my code per Moxy's suggestions as follows:

- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"%s", __FUNCTION__);
    [self performSelector:@selector(transition1)
             withObject:nil
             afterDelay:2.0f];
}

-(void)transition1
{
    NSLog(@"%s", __FUNCTION__);
    UIImage * secondImage = [UIImage imageNamed:@"image2.png"];
    [UIView transitionWithView:self.firstView
                duration:5.0f
                 options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                            self.imageView.image = secondImage;
                }
                completion:^(BOOL finished){
                            [self performSelector:@selector(transition2)
                                       withObject:nil
                                       afterDelay:2.0f];
                }];
}

-(void)transition2
{
    NSLog(@"%s", __FUNCTION__);
    self.patterns = [[PatternViewController alloc] initWithNibName:@"PatternView_iPad"
                                          bundle:nil];
    self.patterns.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:patterns
                 animated:YES
                 completion:nil];
}

Upvotes: 0

Views: 310

Answers (1)

Moxy
Moxy

Reputation: 4212

All you need to do is to start your second animation in the first animation's completion block. Sleep() is something you probably shouldn't be using.

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%s", __FUNCTION__);
    [self performSelector:@selector(transition1)
               withObject:nil
               afterDelay:2.0f]
}

-(void)transition1
{
    NSLog(@"%s", __FUNCTION__);
    UIImage * secondImage = [UIImage imageNamed:@"image2.png"];
    [UIView transitionWithView:self.firstView
                      duration:5.0f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                            self.imageView.image = secondImage;
                    }
                    completion:^(BOOL finished){
                            [self performSelector:@selector(transition2)
                                       withObject:nil
                                       afterDelay:2.0f]
                     }];
}

-(void)transition2
{
    NSLog(@"%s", __FUNCTION__);
    self.patterns = [[PatternViewController alloc] initWithNibName:@"PatternView_iPad" 
                                                            bundle:nil];
    self.patterns.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:patterns
                       animated:YES
                     completion:nil];
}

Upvotes: 3

Related Questions