【问题标题】:iOS UIView Animation cancel doesn't workiOS UIView 动画取消不起作用
【发布时间】:2014-02-10 11:40:37
【问题描述】:

我有两张图片 mosha1 和 mosha2。我通过 UIView 动画将它们从某个位置移动到另一个点 (206,89)。现在我想如果用户在移动时触摸任何图像,该图像的移动就会停在那里。所以我设置了停止动画运动的代码:

[mosha1.layer removeAllAnimations];

这个方法调用停止了图像的移动,但是它显示在点 (206,89) 上。但它应该出现在用户触摸它的确切位置。如何解决这个问题?

UIView 动画方法:

-(void)moshaMoveUp: (UIImageView *)mosha {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
mosha.center = CGPointMake(206, 89);
[UIView commitAnimations];

}

【问题讨论】:

  • 这是一种非常古老的动画制作方法,在 iOS 4.0 之后不鼓励使用。阅读文档。改用基于块的动画。

标签: ios iphone objective-c ios7 uiviewanimation


【解决方案1】:

原因在于动画如何与不同的图层一起工作。开始动画的那一刻,您的对象在逻辑上立即到达目的地。然而,有一个表示层显示对象向目的地移动。 因此,如果您想停止对象,您需要从UIView 的表示层获取其当前位置,将其分配给视图的位置,然后删除动画,您的对象应该在正确的位置。

【讨论】:

    【解决方案2】:

    停止动画后,frame 将成为最终目的地。如果你想把它停在原处,你必须得到视图层的presentationLayer(它包含对象的当前状态,而不是最终状态),并用它来调整有问题的视图的frame

    CALayer *layer = self.animatedView.layer.presentationLayer;
    [self.animatedView.layer removeAllAnimations];
    self.animatedView.frame = layer.frame;
    

    【讨论】:

      【解决方案3】:

      改用这个。您正在做的事情已经很老了,不再受支持。

      [UIView animateWithDuration:3.0
                            delay:0.0
                          options:UIViewAnimationOptionsCurveEaseInOut
                       animations:^() {
                                   mosha.center = CGPointMake(206, 89);
                                  }
                       completion:nil];
      

      这可能会或可能不会解决您的取消问题,但您需要使用这种动画方法。

      我知道这并不能解决问题。但使用最新且未弃用的方法仍然很重要。

      【讨论】:

      • 无论使用什么动画方法,目标位置的问题都会一直存在。
      • 也许可以,但使用明确表示Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.的方法无论如何都不是一件好事。
      • @Fogmeister 的建议真的很有帮助,你应该避免弃用的方法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      相关资源
      最近更新 更多