为循环UIView动画块添加随机性

时间:2013-01-24 18:23:06

标签: iphone ios objective-c ipad uiviewanimation

我正在尝试使用animateWithDuration:方法在iOS中制作动画。

我正在屏幕上移动一个图像(UIImageView中的云的简单图片),然后让这个动画在循环中运行,我想在每次穿过屏幕时改变速度(持续时间)。 / p>

我尝试了几种方法,但两种方式对我都不正确。

我首先虽然可以像这样使用UIViewAnimationOptionRepeat:

[UIImageView animateWithDuration:arc4random() % 10 + 1
                           delay:0.0
                         options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionRepeat
                      animations:^{
 //moving the cloud across the screen here
}
completion:^(BOOL finished) {
    NSLog(@"Done!");
}];

但是这似乎没有再次调用arc4random()来重置持续时间......即。只有每次启动应用程序时,云才会以随机速度穿过屏幕,而不是每次动画循环时。

然后我尝试使用完成块再次像这样触发动画:

-(void)animateMethod
{
[UIImageView animateWithDuration:arc4random() % 10 + 1
                           delay:0.0
                         options:UIViewAnimationOptionCurveLinear
                      animations:^{
 //moving the cloud across the screen here
}
completion:^(BOOL finished) {
    NSLog(@"Done!");
    [self animateMethod];
}];
}

这给了我正在寻找的效果,但是当我使用导航控制器推送到另一个视图时,完成块会在一个无休止的循环中被触发(我的日志被“Done!”发送垃圾邮件)

任何人都知道如何以正确的方式获得理想的效果?

1 个答案:

答案 0 :(得分:5)

你走在正确的轨道上。关键是你需要只在动画结束时才循环,如果它失败了。因此,在告诉它循环之前,您需要检查finished BOOL是否为真。

-(void)animateMethod
{
    [UIImageView animateWithDuration:arc4random() % 10 + 1
                               delay:0.0
                             options:UIViewAnimationOptionCurveLinear
                          animations:^{
     //moving the cloud across the screen here
    }
    completion:^(BOOL finished) {
        if (finished) {
            NSLog(@"Done!");
            [self animateMethod];
        }
    }];
}

这种方法非常适合这种非常简单的动画,当你只做一些时,就像一次可能有3-5个云一样。除此之外,您可能希望使用NSTimer或CADisplayLink设置自己的动画循环并调整其中的云帧。它是一种更加手动的方式,但它甚至可以在UIKit中为您提供一些不错的动画。