繁体   English   中英

iOS UIView动画取消不起作用

[英]iOS UIView Animation cancel doesn't work

我有两个图像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];

}

这样做的原因是动画如何在不同的图层上工作。 开始动画的那一刻,您的对象在逻辑上立即位于目标位置。 但是,有一个表示层,显示对象向目的地移动。 因此,如果要停止对象,则需要从UIView的表示层中获取其当前位置,然后将其分配给视图的位置,然后删除动画,然后将对象放置在正确的位置。

停止动画后,该frame将成为最终目的地。 如果您想将其停在原处,则必须获取视图图层的presentationLayer (其中包含对象的当前状态,而不是最终状态),然后使用它来调整相关的视图frame

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

改用它。 您正在执行的操作非常古老,不再受支持。

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

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

我知道这不能解决问题。 但是使用最新和不推荐使用的方法仍然很重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM