0

I have an animation that plays fine. But at the end of the animation, the animation stops. I want the animation to loop. How can I do this? Here is my code:

    - (void) startTicker
{
    if (self.timerIsRunning) return;

    self.timerIsRunning = YES;

    NSTimer *newTimer = [NSTimer timerWithTimeInterval:(0.1) target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:newTimer forMode:NSDefaultRunLoopMode];

   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:100.0];
   [UIView setAnimationDelegate:self];
   [UIView setAnimationDelay:0.0];
   [UIView setAnimationDidStopSelector:@selector(moveToLeft:finished:context:)];
   [UIView commitAnimations];


}


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

        imageView.left = 800; 
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:100.0];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self cache:YES];
        imageView.right = 800;

        [UIView setAnimationDidStopSelector:@selector(moveToLeft2:finished2:context2:)];
        [UIView commitAnimations];



    }
Richard Povinelli
  • 1,419
  • 1
  • 14
  • 28
Yusuf OZKAN
  • 115
  • 4
  • 8
  • could you edit your question to add in the top lines of code of the first function, which appears to have been cut off? – Michael Dautermann Nov 29 '11 at 14:01
  • i edited dude. How can i solve this problem :( – Yusuf OZKAN Nov 29 '11 at 14:07
  • do you call `moveToLeft:finished:context` from your `moveToLeft2:finished2:context2` method? :-) – Michael Dautermann Nov 29 '11 at 14:10
  • I think you're doing repeating / infinite animations wrong. Look at [this duplicate question](http://stackoverflow.com/questions/3919831/uiview-infinite-loop-animation-to-call-a-method-after-every-repeat-cycle) and see if you can find a solution that works better for you. – Michael Dautermann Nov 29 '11 at 14:31

1 Answers1

1

For loop animation the best way is to use block animations which are a part of UIView.

When calling the method:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

The "option" you probably want is UIViewAnimationOptionAutoreverse and combined with the UIViewAnimationOptionRepeat

For example: (loop blinking red border)

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
    [redBorder setAlpha:0]; //first part of animation
    [redBorder setAlpha:0.5]; //second part of animation
} completion:nil];