2013-05-02 75 views
11

我有3個使用for循環動態創建的視圖。在我呼籲旋轉動畫下面的方法如何停止iPhone中的UIView CABasicAnimation

- (void)runRightSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations: (CGFloat)rotations repeat:(float)repeat; 
{ 
    CABasicAnimation* rotationAnimation; 
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    rotationAnimation.fromValue = [NSNumber numberWithFloat:0]; 
    rotationAnimation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)]; 
    //rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ]; 
    rotationAnimation.duration = duration; 
    //rotationAnimation.cumulative = YES; 
    rotationAnimation.repeatCount = repeat; 

[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
} 

如何停止在觸摸旋轉動畫開始的看法?... 我嘗試下面的編碼在觸摸開始的方法,但它沒有工作

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    int viewTag=[touch view].tag; 
    UIView *myView =(UIView *)[self.view viewWithTag:viewTag]; 
    [myView.layer removeAllAnimations]; 
} 

請幫我...

+1

的可能重複(http://stackoverflow.com/questions/2306870/is-there -a路 - 間歇-A-cabasicanimation) – Pfitz 2013-05-02 11:14:54

回答

23

這可能是因爲你是從其他層去除動畫,而不是從你上添加了動畫同一層刪除動畫。 (您正在使用錯誤的視圖來移除動畫,請再次檢查它)。

您可以用它來從yourView刪除動畫:?有沒有辦法暫停CABasicAnimation]

[yourView.layer removeAllAnimations];