停止 UIView 动画。

3

可能是重复问题:
如何取消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];

} 
4个回答

6

试试这个:

[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值非常小,主线程也能响应您的按键操作。

0

您可以只改变当前正在进行动画的对象/属性,它将停止动画。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接