反复和连续地动画视图

时间:2013-11-12 13:46:45

标签: ios objective-c animation uiviewanimation caemitterlayer

我正在模拟一些效果,比如落叶,下雪和下雨。 我的第一个电话是使用CAEmitterLayer,它工作得非常好,但是我需要在上下文中渲染图层以保存为图像,并且看不到,或者至少非常复杂。

所以我正在使用一些视图动画。我需要不断为我的imageViews制作动画,这样它就可以通过屏幕掉落并重新出现在它上面,每一个都有不同的速度。

所以我用animateWithDuration在屏幕上重新定位每个imageView,当动画完成时,我调用从完成块递归执行该操作的方法,这样imageview可以使它到达屏幕的末尾。一旦imageView到达屏幕的末尾,我就将其重新定位在顶部。

问题是当动画结束时我的imageView会停止一点,直到完成块递归调用该方法,我希望它是连续的。

我的代码为每个imageView生成随机位置,我的动画持续时间非常短,所以我总是可以更新imageView的位置,以防用户点击按钮将视图保存为图像。

无论如何,这是我的代码:

- (void)effectOnObject:(UIImageView *)object{
    NSInteger initialX;
    NSInteger initialy;
    CGFloat duration;

    //the object have reached the end of screen
    if (object.frame.origin.y >= self.frame.size.height) {
        //generate random position to relocate the object on x axis
        initialX = arc4random() % 321;

        //generate random position to relocate the object on y axis (little bit above top of screen)
        initialy = ((NSInteger)-object.frame.size.height) - arc4random() % 11;

        //duration 0 so the object can be replaced without animation
        duration = 0.0;
    }
    else{
        initialX = object.frame.origin.x;

        //this change the speed of object
        initialy = object.frame.origin.y + 10

        //setted duration to short time
        duration = 0.01;
    }

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionAllowAnimatedContent 
     animations:^{
        [object setFrame:CGRectMake(initialX, initialy, object.frame.size.width, object.frame.size.height)];
     }
     completion:^(BOOL completed){
         //recursively call
         [self effectOnObject:object];                   
      }];
}

- (void)startEffect{

    //initiate effect on each imageView
    for (UIImageView *object in self.subviews) {
        [self effectOnObject:object];   
    }    
}

如果从完成块递归调用方法并重新开始动画,我怎么能连续不断地制作这个动画呢?

2 个答案:

答案 0 :(得分:1)

当对象到达屏幕末尾时,您无需执行0持续时间的动画。只需在完成块中测试它,然后在递归调用之前重新定位视图。此外,0.01的持续时间似乎有点极端(每秒100帧)。也许像0.1这样更合理的价值会有所帮助。

这样的事情:

- (void)effectOnObject:(UIImageView *)object{

    //setted duration to short time
    CGFloat duration = 0.1;

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionAllowAnimatedContent
                     animations:^{

                         NSInteger initialX = object.frame.origin.x;

                         //this change the speed of object
                         NSInteger initialy = object.frame.origin.y + 10;

                         [object setFrame:CGRectMake(initialX, initialy, object.frame.size.width, object.frame.size.height)];
                     }
                     completion:^(BOOL completed){

                         NSInteger initialX = arc4random() % 321;

                         //generate random position to relocate the object on y axis (little bit above top of screen)
                         NSInteger initialy = ((NSInteger)-object.frame.size.height) - arc4random() % 11;

                         [object setFrame:CGRectMake(initialX, initialy, object.frame.size.width, object.frame.size.height)];

                         //recursively call
                         [self effectOnObject:object];
                     }];
}

答案 1 :(得分:0)

从我简要阅读的内容中,您应该查看CAReplicatorLayer。这是重复的东西!我能想到的唯一问题是变换和价值必须是增量的......

相关问题