开发者

Repeat beginAnimations -- All Animations

开发者 https://www.devze.com 2023-02-16 17:26 出处:网络
I\'m making a simple image rotator by fading in and out 4 different images. It works great, until the end, when I want all four animations to repeat.

I'm making a simple image rotator by fading in and out 4 different images. It works great, until the end, when I want all four animations to repeat.

I've tried the repeat count, but that will only apply to that one specific animation. How do I create an entire loop?

My code is below. Thanks in advance!

[UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:slideshow cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:3];
    [UIView setAnimationRepeatAutoreverses:YES];

    [UIView setAnimationDelay:3];
    imageOne.alpha = 0;
    imageTwo.alpha = 1;

    [UIView setAnimationDelay:6];
    开发者_C百科imageTwo.alpha = 0;
    imageThree.alpha = 1;

    [UIView setAnimationDelay:9];
    imageThree.alpha = 0;
    imageFour.alpha = 1;

    [UIView commitAnimations];


You can start by putting a name to your animations so instead of beginAnimations:nil, you'd put beginAnimations: @"Button Animations" or something like that. Also, you should set the delegate of the animation to self. This can be done by adding the following line of code into your animation block:

 [UIView setAnimationDelegate: self];

Then you can put this whole entire animation block into a class method of it's own (in your view controller). Lets call it doAnimation. This is just for safe measure. the next thing you should do is implement this delegate function in your view controller:

 -(void) animationDidStop: (NSString *) animationID finished:(NSNumber *)finished context:(void *)context{

    if([animationID isEqualToString:@"Button Animation"]){
         [self doAnimation];
    }
 }

So pretty much this will ensure that your animations will repeat. If this method doesn't work, let me know, I didn't test it. But I think it'll work.

0

精彩评论

暂无评论...
验证码 换一张
取 消