动画未按编写代码的顺序运行

时间:2014-08-02 09:47:41

标签: ios uiviewcontroller uiimageview uibutton

我正在尝试设置一个按钮,在图像视图中播放动画,该按钮是我的根视图控制器的一部分,然后继续更改子视图控制器。

但由于某种原因,代码会更改视图控制器,然后播放动画。

我认为动画片段会在视图控制器发生变化之前实现,因为它是代码中的第一个。

代码做的一切都很好,它只是向后做。任何人都有任何关于出错的想法?​​

- (IBAction)next:(UIButton *)sender {

    clouds.animationImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed:@"cloudAnim.0001.png"],
                              [UIImage imageNamed:@"cloudAnim.0002.png"],
                              [UIImage imageNamed:@"cloudAnim.0003.png"],
                              [UIImage imageNamed:@"cloudAnim.0004.png"],
                              [UIImage imageNamed:@"cloudAnim.0005.png"],
                              [UIImage imageNamed:@"cloudAnim.0006.png"],
                              [UIImage imageNamed:@"cloudAnim.0007.png"],
                              [UIImage imageNamed:@"cloudAnim.0008.png"],
                              [UIImage imageNamed:@"cloudAnim.0009.png"],
                              [UIImage imageNamed:@"cloudAnim.0010.png"],
                              [UIImage imageNamed:@"cloudAnim.0011.png"],
                              [UIImage imageNamed:@"cloudAnim.0012.png"],
                              [UIImage imageNamed:@"cloudAnim.0013.png"],
                              [UIImage imageNamed:@"cloudAnim.0014.png"],
                              [UIImage imageNamed:@"cloudAnim.0015.png"],
                              [UIImage imageNamed:@"cloudAnim.0016.png"],
                              [UIImage imageNamed:@"cloudAnim.0017.png"],
                              [UIImage imageNamed:@"cloudAnim.0018.png"],
                              [UIImage imageNamed:@"cloudAnim.0019.png"],
                              [UIImage imageNamed:@"cloudAnim.0020.png"],
                              [UIImage imageNamed:@"cloudAnim.0021.png"],
                              [UIImage imageNamed:@"cloudAnim.0022.png"],
                              [UIImage imageNamed:@"cloudAnim.0023.png"],
                              [UIImage imageNamed:@"cloudAnim.0024.png"],
                              [UIImage imageNamed:@"cloudAnim.0025.png"],
                              [UIImage imageNamed:@"cloudAnim.0026.png"],
                              [UIImage imageNamed:@"cloudAnim.0027.png"],
                              [UIImage imageNamed:@"cloudAnim.0028.png"],
                              [UIImage imageNamed:@"cloudAnim.0029.png"],
                              [UIImage imageNamed:@"cloudAnim.0030.png"],
                              [UIImage imageNamed:@"cloudAnim.0031.png"],
                              [UIImage imageNamed:@"cloudAnim.0032.png"],
                              [UIImage imageNamed:@"cloudAnim.0033.png"],
                              [UIImage imageNamed:@"cloudAnim.0034.png"],
                              [UIImage imageNamed:@"cloudAnim.0035.png"],
                              [UIImage imageNamed:@"cloudAnim.0036.png"],
                              [UIImage imageNamed:@"cloudAnim.0037.png"],
                              [UIImage imageNamed:@"cloudAnim.0038.png"],
                              [UIImage imageNamed:@"cloudAnim.0039.png"],
                              [UIImage imageNamed:@"cloudAnim.0040.png"],
                              [UIImage imageNamed:@"cloudAnim.0041.png"],
                              [UIImage imageNamed:@"cloudAnim.0042.png"],
                              [UIImage imageNamed:@"cloudAnim.0043.png"],
                              [UIImage imageNamed:@"cloudAnim.0044.png"],
                              [UIImage imageNamed:@"cloudAnim.0045.png"],
                              [UIImage imageNamed:@"cloudAnim.0046.png"],
                              [UIImage imageNamed:@"cloudAnim.0047.png"],
                              [UIImage imageNamed:@"cloudAnim.0048.png"], nil];
    [clouds setAnimationRepeatCount:1];
    clouds.animationDuration = 2.0;
    [clouds startAnimating];

    if (currentPage == 1){
        self.page02ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"page02"];
        [self.view insertSubview:_page02ViewController.view atIndex:0];
        [self.page01ViewController.view removeFromSuperview];
    }

    if (currentPage == 2){
        self.page03ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"page03"];
        [self.page02ViewController.view removeFromSuperview];
        [self.view insertSubview:_page03ViewController.view atIndex:0];
    }

    currentPage = currentPage +1;
    if (currentPage >3){
        currentPage = 3;
    }
}

1 个答案:

答案 0 :(得分:0)

动画是异步的,这意味着下面的代码将立即执行。

如果您想等到动画的一个循环完成,那么您需要在之后延迟代码。最简单的方法是使用dispatch_after

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // Your view controller switching code will be executed after 2 seconds...
});

请注意,由于该方法立即返回,因此没有什么能阻止用户再次点击该按钮并排队另一个转换。您可能希望在转换期间禁用用户交互,但如果用户必须经常切换,则2秒的转换对用户来说将是永恒的。