7

I want to repeat an animation forever in my view. Below is my code:

[UIView animateWithDuration:0.5f
        delay:0.49f
        options:UIViewAnimationOptionCurveEaseInOut
        animations:^{
        //for IMMERSE
        [_pic2 setAlpha:1.0f];
         _view1_immerse=_pic2.frame;
         _view1_immerse.origin.x = ([UIScreen mainScreen].bounds.size.width-_view1_immerse.size.width)/2;
         self.pic2.frame=_view1_immerse;
         }
         completion:^(BOOL finished){
         [UIView animateWithDuration:0.5f delay:0.49f options:UIViewAnimationOptionCurveEaseOut animations:^{
         _view1_immerse=_pic2.frame;
         _view1_immerse.origin.x = [UIScreen mainScreen].bounds.size.width;
         self.pic2.frame=_view1_immerse;
         [_pic2 setAlpha:0.0];
         } completion:^(BOOL finished){
         [UIView animateWithDuration:0.0f animations:^{
         _view1_immerse=_pic2.frame;
         _view1_immerse.origin.x = -_view1_immerse.size.width;
         self.pic2.frame=_view1_immerse;
                                     }];
                                 }];
                             }];

I tried adding option UIViewAnimationOptionRepeat, but it seems that it only repeats the "animation" part of the most outside layer. I want to repeat the whole set of animation.

And I do not want to use NSTimer. Could anyone give me some advice about my problem?? Thanks in advance!

Jenny Cheung
  • 301
  • 2
  • 5
  • 12
  • Put your `UIViewAnimationOptionRepeat` on the inner animations instead of the upper animation. If that doesn't work, use keyframe animations instead. – brandonscript Jul 15 '15 at 04:13

1 Answers1

30

You can check below code for repeat animation.

[UIView animateWithDuration:1.0f
                  delay:0.0f
                options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
             animations: ^(void){self.view.center = CGPointMake(0, 200);}
             completion:NULL];

Thanks


Swift 4

UIView.animate(withDuration: 100,
                   delay: 0,
                   options: [.repeat, .autoreverse, .beginFromCurrentState],
                   animations: {

    }, completion: nil)
Hindu
  • 2,894
  • 23
  • 41