【问题标题】:Stop an UIView Animation [duplicate]停止 UIView 动画 [重复]
【发布时间】:2012-06-18 22:32:23
【问题描述】:

可能重复:
Cancel a UIView animation?

我有以下动画,我想在按下按钮时停止它。你能告诉我我该怎么做吗?我无法弄清楚如何。谢谢。

-(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];

} 

【问题讨论】:

    标签: objective-c ios xcode uibutton uiviewanimation


    【解决方案1】:

    试试这个:

    [myView.layer removeAllAnimations];
    

    并确保同时导入 Quartz 框架:

    #import <QuartzCore/QuartzCore.h>
    

    动画是使用 QuartzCore 框架中的 CoreAnimation 应用的,甚至是 UIView 动画方法。只需定位要停止动画的视图层,然后删除其动画即可。

    【讨论】:

      【解决方案2】:

      你可以这样做:

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

      这比调用[self.view.layer removeAllAnimations] 更有效,因为这可能会删除您想要保留的其他动画。

      【讨论】:

        【解决方案3】:

        您可以只更改当前动画的对象/属性。它将停止动画。

        【讨论】:

          【解决方案4】:

          我建议你使用 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 值非常小,主线程也可以在您按下时响应

          【讨论】:

            猜你喜欢
            • 2023-04-08
            • 2019-09-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-10-25
            • 1970-01-01
            相关资源
            最近更新 更多