如何在iOS中永远重复动画?

Jen*_*ung 3 animation objective-c ios

我想在我看来永远重复一个动画.以下是我的代码:

[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;
                                     }];
                                 }];
                             }];
Run Code Online (Sandbox Code Playgroud)

我尝试添加选项UIViewAnimationOptionRepeat,但它似乎只重复最外层的"动画"部分.我想重复一整套动画.

我不想用NSTimer.谁能给我一些关于我的问题的建议?提前致谢!

Hin*_*ndu 22

您可以查看以下代码以重复动画.

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

谢谢


斯威夫特4

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

    }, completion: nil)
Run Code Online (Sandbox Code Playgroud)

  • `[view.layer removeAllAnimations]` (9认同)