【问题标题】:Stop Repeating UIView Animation Block?停止重复 UIView 动画块?
【发布时间】:2023-04-08 07:11:01
【问题描述】:

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

我已经尝试过[dot1.layer removeAllAnimations];,但它在没有返回到原始位置的情况下停止了它的轨道,它停止了中间动画。

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

那么是否可以在动画完成最后一个周期而不是中途后简单地停止动画?

谢谢。

【问题讨论】:

  • 有点小技巧,但是你可以设置一个@property bool,当你想要停止动画和动画块完成时,检查 bool 是否为真,然后 removeAllAnimations 并调用你的下一个动画。您可以读取块中的属性,而不是设置它们,就在那里思考。希望其他人可以对此给出正确的答案。
  • @BooRanger 这不是黑客,而是答案!

标签: iphone objective-c ipad animation uiview


【解决方案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];

【讨论】:

    【解决方案2】:

    我已经尝试了公认的解决方案,但它对我不起作用。动画在一次运行后就停止了。在网上挖掘让我相信[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;
    }
    

    希望这对某人有所帮助。 :)

    【讨论】:

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