2012-06-18 77 views
3

可能重複:
Cancel a UIView animation?停止的UIView的動畫

我有以下的動畫,我想阻止它,當我按下一個按鈕。你能告訴我我該怎麼做?我無法弄清楚如何。謝謝。

-(void)animationLoop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
[UIView beginAnimations:@"Move randomly" context:nil]; 
[UIView setAnimationDuration:1]; 
// [UIView setAnimationBeginsFromCurrentState:NO]; 


CGFloat x = (CGFloat) (arc4random() % (int) self.bounds.size.width); 
CGFloat y = (CGFloat) (arc4random() % (int) self.bounds.size.height); 

CGPoint squarePostion = CGPointMake(x, y); 
strechView.center = squarePostion; 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)]; 

[UIView commitAnimations]; 

} 

回答

0

您可以更改當前動畫的對象/屬性。它會停止動畫。

5

試試這個:

[myView.layer removeAllAnimations]; 

,並確保導入Quartz框架,以及:

#import <QuartzCore/QuartzCore.h> 

的動畫在QuartzCore框架使用CoreAnimation,甚至UIView的動畫方法應用。只需定位視圖的要停止動畫的圖層,然後刪除其動畫。

3

你可以這樣做:

[self.view.layer removeAnimationForKey:@"Move randomly"]; 

這比調用[self.view.layer removeAllAnimations],因爲這將有可能刪除您想保留其他動畫更加有效。

0

我建議你使用NSTimer。

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(yourAnimation) userInfo:nil repeats:YES]; 

- (void)responseToButton:(UIButton *) _button { 
    //when you want to stop the animation 
    [myTimer invalidate]; 
} 

- (void)yourAnimation { 
     // move the view to a random point 
} 

此外,你可以設置的NSTimer工作在另一個線程,這樣當你按下主線程響應甚至你的價值TimeInterval所非常小