2013-07-16 143 views
1

我正在對圖像執行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]; 

} 
+0

我從其他帖子找到了答案。 它使用以下內容: [self performSelector:@selector(metohodToPushView)withObject:nil afterDelay:0.8]; – firewall

+1

這不是一個很好的方法來實現這一點。使用performSelector:withObject:afterDelay:是一個破解。查看關於animateWithDuration的答案:動畫:completion :. –

+0

...和'CGAffineTransformRotate',您可以使用它來旋轉視圖的'transform'屬性。 – CouchDeveloper

回答

4

使用的UIView的animateWithDuration:動畫:完成:方法。把你的電話打到執行SegueWithIdentifier: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]; 
    }]; 
+0

我試過了,但並不等待1.5秒。它直接切換觀點。 我在我的第一篇文章中添加了我的整個動畫代碼,直到切換視圖。 – firewall

+0

你在那裏添加CABasicAnimation代碼嗎?這不應該是必要的,甚至可能會干擾animateWithDuration。請參閱我給出的關於如何使用animateWithDuration進行旋轉轉換的示例中的更新代碼。 –

相關問題