停止重复UIView动画块?

时间:2013-02-01 14:53:06

标签: iphone objective-c ipad animation uiview

我有一个UIView动画块,它使用UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoReverse选项,所以它继续前进,但在某些时候我需要停止动画,但更重要的是停止它并使视图返回其原始位置。

我已经尝试了[dot1.layer removeAllAnimations];,但是在没有将其恢复到原始位置的情况下将其停在轨道上,它会在动画中途停止。

我已经尝试停止它然后只是运行第二个动画将它返回到正确的帧,但这可能有点不稳定,因为我不知道它需要多长时间才能使用等等。

那么可以简单地让动画在完成最后一个循环而不是中途之后停止吗?

感谢。

2 个答案:

答案 0 :(得分:1)

你应该试试这个:

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^
{
    view.frame = CGRectMake(0, 100, 200, 200);
} completion:^(BOOL finished)
{
    if(! finished) return;
}];

要停止动画,请使用:

[view.layer removeAllAnimations];

答案 1 :(得分:0)

我已尝试过已接受的解决方案但它对我没用。一次运行后动画停止了。在网上挖掘让我相信[self.view.layer removeAllAnimations]是推荐停止动画的方法(另一种方法可以在以下链接中看到,因为提问者对已接受的解决方案的评论

Stop an auto-reverse / infinite-repeat UIView animation with a BOOL / completion block)。

对我有用的方式是BooRanger给出的方式(甚至jrturton接受了它)。我在类中设置了一个名为stopAnimations的布尔属性,并将其值初始化为NO。每当我想停止动画时,我将stopAnimations设置为YES,然后在下一行中调用[self.view.layer removeAllAnimations]。然后在完成块中,我执行以下操作。

if(self.stopAnimations){
self.finishAnimations = NO;
return;
}

希望这有助于某人。 :)