user1253528
user1253528

Reputation:

UIView multiple animations with different durations

I have a background UIImageView which is constantly animating with this code that gets called at the end of 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];
}

The problem is, when I perform another animation, such as this:

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

The background animation resets to it's original state and stops animating..

Thank you in advance!

Upvotes: 0

Views: 488

Answers (1)

Sujith Thankachan
Sujith Thankachan

Reputation: 3506

Try this:

Keep a bool variable globally like: BOOL isFirstAnimationCompleted; and set it false in the starting of the first animation.

[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;                           
                           }

                         }];

Check while the user wants to perform the second animation like:

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

Upvotes: 1

Related Questions