动画结束后执行操作

我在图像上执行动画,我将其旋转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]; } 

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