动画结束后执行操作

时间:2013-07-16 20:57:18

标签: ios animation switch-statement wait

我正在我将图像旋转180度的图像上执行动画。

然后我插入了一个代码来切换到另一个视图。

但是,应用程序在动画结束前切换视图。

如何等待动画完成以便之后切换视图?

这是我的代码:

- (IBAction)buttonPressed:(id)sender
{
    CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

    imageRotation.fromValue = [NSNumber numberWithFloat:0];
    imageRotation.toValue = [NSNumber numberWithFloat:((180*M_PI)/ 180)];

    imageRotation.duration = 1.5;
    imageRotation.repeatCount = 1;

    imageRotation.removedOnCompletion = NO;
    imageRotation.autoreverses=NO;
    imageRotation.fillMode = kCAFillModeForwards;

    [_image.layer addAnimation:imageRotation forKey:@"imageRotation"];

    // Switching Views
    [self performSegueWithIdentifier: @"ToGalleryVCSegue" sender: self];

}

1 个答案:

答案 0 :(得分:5)

使用UIView的animateWithDuration:animations:completion:方法。在完成块中调用performSegueWithIdentifier:sender:。例如:

[UIView animateWithDuration:1.5
                 animations:^{
        // put your animation code here, eg:
        CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
        self.imageView.transform = transform;
    }
                 completion:^{
        [self performSegueWithIdentifier: @"ToGalleryVCSegue" sender: self];
    }];