UILabel动画循环

Yul*_*ega 1 animation objective-c uikit ios

我正在尝试制作一个动画,其中文本在淡出时移动到底部,同时它再次出现在与之前相同的位置并执行相同的动画.我的代码:

for (int i=0; i<10; i++)
{
    [UIView animateWithDuration:0.5
                          delay:0.2f
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         productTextLabel.center = CGPointMake(381, 410);
                         productTextLabel.alpha = 0.0;
                         productTextLabel.center = CGPointMake(381, 340);
                         productTextLabel.alpha = 1;
                     }
                     completion:^(BOOL fin) {
                     }];

}
Run Code Online (Sandbox Code Playgroud)

我的问题是我试图使这个动画发生不止一次.我正在使用for循环,但它只执行一次.

Rob*_*jke 14

您可以在animationWithDuration:方法中使用选项UIViewAnimationOptionRepeat和UIViewAnimationOptionAutoReverse来自动重复和反转动画.

整个方法将成为:

[UIView animateWithDuration:0.5
                      delay:0.2f
                    options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoReverse
                 animations:^{
                     productTextLabel.center = CGPointMake(381, 410);
                     productTextLabel.alpha = 0.0;
                 }
                 completion:^(BOOL fin) {
                 }];
Run Code Online (Sandbox Code Playgroud)

要取消动画,您可以拨打电话

[productTextLabel.layer removeAllAnimations];
Run Code Online (Sandbox Code Playgroud)

注意:您应该导入QuartzCore以便能够调用removeAllAnimations函数.