UIView具有不同持续时间的多个动画

时间:2014-03-27 10:02:09

标签: ios objective-c uiview uiviewanimation

我有一个背景UIImageView,它通常会在ViewDidAppear结束时调用此代码:

- (void)beginBackgroundAnimation
{
[UIView animateWithDuration:45
                      delay:0
                    options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^{
                     _background.frame = CGRectMake(-_background.frame.size.width + self.view.frame.size.width,
                                                    _background.frame.origin.y,
                                                    _background.frame.size.width,
                                                    _background.frame.size.height);
                 }
                 completion:nil];
}

问题是,当我执行另一个动画时,例如:

[UIView animateWithDuration:delay animations:^{
            registerRowView.alpha = 1;
            registerRowView.frame = newFrame; // this line kills the animation
        }];

背景动画重置为原始状态并停止制作动画..

提前谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

将bool变量全局保存为:BOOL isFirstAnimationCompleted;并在第一个动画的开头设置false

[UIView animateWithDuration:45
                          delay:0
                        options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{
                         _background.frame = CGRectMake(_background.frame.size.width + self.view.frame.size.width,
                                                        _background.frame.origin.y,
                                                        _background.frame.size.width,
                                                        _background.frame.size.height);
                     }
                     completion:^(BOOL finished) {
                           if(finished) {
                               isFirstAnimationCompleted = finished;                           
                           }

                         }];

在用户想要执行第二个动画时检查,如:

if(isFirstAnimationCompleted) {
   [UIView animateWithDuration:delay animations:^{
            registerRowView.alpha = 1;
            registerRowView.frame = newFrame; // this line kills the animation
        }];
}