Rashid
Rashid

Reputation: 804

How to get multiple animations to play in order infinitely?

I have three UIViews that i want to animate in a certain order (It is supposed to look like a news ticker with three different labels moving from above then display and then move all the way to the outside of the view). Now this whole process is supposed to repeat infinitely.

This is the code i used:

[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^
{

    [morningPrayerView setFrame:CGRectMake(0, 0, 320, 30)];
    [morningPrayerView setAlpha:1.f];

}
                 completion:^(BOOL finished)
{
    [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{

        [morningPrayerView setFrame:CGRectMake(0, 30, 320, 30)];
        [morningPrayerView setAlpha:1.f];

    } completion:^(BOOL finished) {
        {
            [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{

                [middayPrayerView setFrame:CGRectMake(0, 0, 320, 30)];
                [middayPrayerView setAlpha:1.f];

            } completion:^(BOOL finished) {
                {
                    [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{

                        [middayPrayerView setFrame:CGRectMake(0, 30, 320, 30)];
                        [middayPrayerView setAlpha:1.f];

                    } completion:^(BOOL finished) {
                        {
                            [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{
                                [eveningPrayerView setFrame:CGRectMake(0, 0, 320, 30)];
                                [eveningPrayerView setAlpha:1.f];


                            } completion:^(BOOL finished) {
                                {
                                    [UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveLinear animations:^{

                                        [eveningPrayerView setFrame:CGRectMake(0, 30, 320, 30)];
                                        [eveningPrayerView setAlpha:1.f];

                                    } completion:^(BOOL finished) {
                                        //this block is called when the animation is over
                                    }];

                                }                                }];

                        }                        }];

                }                }];

        }        }];

}];

The problem is that only the first animation gets the go and none of the other animations start.

Upvotes: 0

Views: 191

Answers (1)

zoul
zoul

Reputation: 104145

IMHO the UIViewAnimationOptionRepeat switch means to repeat the animation indefinitely, therefore your first animation loops forever and never calls the completion handler. Simply wrap the whole animation setup in method, remove the repeat switches and when the last completion handler is executed, call the method again to repeat the whole show? Something like this:

- (void) animateView {
    [UIView animateWithDuration:5.0 animations:^{
        // …
    } completion:^(BOOL finished) {
        [self animateView];
    }];
}

That’s probably a retain cycle, but as long as you cancel the animation eventually, it should be OK.

Upvotes: 1

Related Questions