【问题标题】:Repeat an animation a variable number of times以可变次数重复动画
【发布时间】:2011-08-14 22:55:34
【问题描述】:

我想知道如何将动画设置为重复。重复次数需要由变量确定。在下面的代码中,变量int newPage 应该决定动画的重复频率。

我试过了,但动画(使用块动画)只执行了一次:

for (int temp = 1; temp <= newPage; temp++) {
            [self animatePage];
}

如果我编写以下代码,它会像我想要的那样工作,但这是硬编码的(即动画将重复两次),我看不出如何更改此动画的频率在代码中执行并根据我的变量 newPage:

[UIView animateWithDuration:0
                      delay:0.1
            options:UIViewAnimationOptionCurveEaseIn
             animations:^{[self animatePage];}
             completion:^(BOOL finished){[self animatePage];}];

非常感谢有关如何重复相同动画的建议,而无需硬编码我希望此动画重复的次数。


编辑:

我尝试实现以下代码,但实际上只会执行一个动画:

        [UIView animateWithDuration:0
                          delay:1
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{

                         [UIView setAnimationRepeatCount:2];
                         [self animatePage];

                     }
                     completion:nil];

【问题讨论】:

  • 你知道,我很确定“removeAllAnimations”就是你想要的——请看下面的答案,干杯!

标签: iphone objective-c cocoa-touch animation


【解决方案1】:

有同样的问题 - 你错过了一个'UIViewAnimationOptionRepeat'

这应该可行:

 [UIView animateWithDuration:0
                      delay:1
                    options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat
                 animations:^{
                     [UIView setAnimationRepeatCount:2]; // **This should appear in the beginning of the block**
                     [self animatePage];

                 }
                 completion:nil];

帮我搞定了。

【讨论】:

  • 太奇怪了,如果不将修改的内容放在动画块的开头会导致无限重复。
【解决方案2】:

您是否尝试设置repeatCount? + (void)setAnimationRepeatCount:(float)repeatCount

我已经尝试了以下代码块,它对我来说肯定重复了 2 倍(我是一个 UITextView,在 X 目录中放大了 2 倍,在 Y 目录中放大了 3 倍):

[UIView animateWithDuration:2 
delay:0.1 
options:UIViewAnimationOptionCurveEaseIn 
animations:^{ [UIView setAnimationRepeatCount:2]; 
l.transform = CGAffineTransformMakeScale(2,3); } completion:nil];

【讨论】:

  • 没有。好主意!但是我需要如何实现呢?对不起,如果这是基本的,但我对这一切都很陌生。
  • @DavidNeiss:我刚试过这个,但它不起作用。如果我将其设置为 2,则只会执行一个动画。我相应地更新了上面的代码,所以你可以看到我是如何尝试实现它的。感谢您的帮助。
  • @n.evermind:你有没有试过setAnimationRepeatCount: 之前 animateWithDuration:,即不在animations块中?
  • @Josh Caswell:不,因为文档说您应该从内部调用它:“如果从动画块外部调用此方法,则不会执行任何操作。您可以将此方法与基于块的方法或用于定义动画块的开始/提交方法。...如果您将 UIViewAnimationOptionRepeat 选项传递给 animateWithDuration:delay:options:animations:completion: 方法而不设置显式重复计数,则动画将无限重复。如果您希望动画重复有限次数,请在您的块内调用此方法。"
  • @Josh Caswell:我实际上解决了它:我调用的另一个动画比我指定的动画持续时间长......我的错。
【解决方案3】:

您没有看到一个动画的原因是,由于您从同一个 runloop 中的循环调用动画,导致最后一次调用获胜(一个动画)

不要调用[self animatePage],而是尝试调用

[self performSelector:@selector(animatePage) withObject:nil afterDelay:.1 *temp];

这将在单独的线程上创建您的调用。

您可能需要使用延迟间隔

【讨论】:

  • 我明白你的意思,但这不适用于短时间间隔。我真的需要 0.1,而您建议的代码行仅适用于 1.0 及更高版本(这太慢了)。不过,感谢您的澄清!
【解决方案4】:

为避免动画之间的打嗝,请使用关键帧。我写了一个扩展,适用于任何符合 UIView 的东西(这是大量的视觉元素)

斯威夫特 4:

import UIKit
extension UIView{
    func rotate(count: Float, _ complete: @escaping ()->()) {
        UIView.animateKeyframes(withDuration: 1.0, delay: 0, options: [.repeat], animations: {
            //to rotate infinitely, comment the next line
            UIView.setAnimationRepeatCount(count)
            //rotate the object to 180 degrees
            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5/1.0, animations: {
                self.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi)))
            })
            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5/1.0, animations: {
                //rotate the object from 180 to 360 degrees
                self.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2)))
            })
        }, completion:{ _ in
            complete()
        })
    }
}

在应用程序的任何地方调用它:

view.rotate() { /*do after*/ }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    相关资源
    最近更新 更多