2012-03-17 82 views
3

我有一個動畫,需要重複,直到我決定停止它。iOS - 停止重複的UIAnimation?

單擊按鈕後如何停止動畫?

[UIView animateWithDuration:0.2 delay:0 options:(UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{ 

     CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(5)); 
     self.transform = transform; 

    } completion:^(BOOL finished){ 

    }]; 
+3

可能的重複[如何有一個處理程序重複UIView animateWithDuration?](http://stackoverflow.com/questions/6766955/how-to-have-a-handler-to-repeat-uiview-animatewithduration ) – sch 2012-03-18 00:07:28

回答

2

ü可以使用CABasicAnimation代替。

CABasicAnimation *appDeleteShakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
appDeleteShakeAnimation.autoreverses = YES; 
appDeleteShakeAnimation.repeatDuration = HUGE_VALF; 
appDeleteShakeAnimation.duration = 0.2; 
appDeleteShakeAnimation.fromValue = [NSNumber numberWithFloat:-degreeToRadian(5)]; 
appDeleteShakeAnimation.toValue=[NSNumber numberWithFloat:degreeToRadian(5)]; 
[self.layer addAnimation:appDeleteShakeAnimation forKey:@"appDeleteShakeAnimation"]; 

然後當u想停止它,你可以叫

[self.layer removeAnimationForKey:@"appDeleteShakeAnimation"]; 
12

您必須添加另外一個選擇UIViewAnimationOptionAllowUserInteraction ...

你應該試試這個:

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction animations:^ 
{ 
    view.frame = CGRectMake(0, 100, 200, 200); 
} completion:^(BOOL finished) 
{ 
    if(! finished) return; 
}]; 

並停止動畫使用此:

[view.layer removeAllAnimations]; 
+1

對具體問題的反應很好,沒有提出另一種有生命觀的方法。做得好。 – SSemashko 2014-01-17 07:58:32

+0

@Scarmysun:謝謝你的讚賞 – 2014-01-17 10:35:45