iOS - 如何按顺序执行两个转换?

时间:2013-03-02 23:24:45

标签: ios uiview uiimageview uiimage uiviewanimation

我从其他SO问题中获取了一些代码,但我必须做一些不可思议的事情。应用程序从第一个视图跳转到第三个视图。我想要实现的是:

ViewController 1图像1 - 加载图像。快速交叉解析为ViewController 1图像2。

ViewController 1 Image 2 - 翻转到ViewController 2。

交叉解决发生但是带我去了VC2。我一天中的大部分时间都在这里受到重创。我坐在浴缸里时,是时候寻求帮助了。

以下是我一直在做的事情:

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

感谢您的帮助。

更新

我根据Moxy的建议更新了我的代码如下:

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

1 个答案:

答案 0 :(得分:3)

您需要做的就是在第一个动画的完成块中开始第二个动画。 Sleep()是您可能不应该使用的东西。

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