如何使用 UIViewAnimationOptionRepeat 自动重复一组链接的 UIView 动画 [英] How to autorepeat a set of chained UIView animations using UIViewAnimationOptionRepeat

查看:33
本文介绍了如何使用 UIViewAnimationOptionRepeat 自动重复一组链接的 UIView 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用标志 UIViewAnimationOptionRepeat 链接一组 UIView animateWithDuration:我试图重复的动画步骤是:

I am trying to chain a set of UIView animateWithDuration: using the flag UIViewAnimationOptionRepeat The animation steps I am trying to repeat are:

  1. 动画 UIView 向左 100 像素
  2. 淡出UIView (opacity '0')
  3. 淡出(不可见)时,将其移回原始位置(向右 100 像素)
  4. 淡入淡出 UIView(将不透明度设置为 '1')
  5. 使用标志UIViewAnimationOptionRepeat
  6. 重复动画
  1. Animate a UIView 100 pixels to the left
  2. Fade the UIView out (opacity '0')
  3. While faded out (invisible), move it back to the original position (100 px to the right)
  4. Fade the UIView back in (set opacity to '1')
  5. Repeat the animation using flag UIViewAnimationOptionRepeat

我无法重复整个动画链 - 只能重复最上面的动画.

I am not able to repeat the whole chain of animations - and only able to repeat the top most animation.

我试过的代码是这样的:

The code I have tried is this:

[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat animations:^{
        self.handImageView.frame = CGRectMoveByXPixels(self.handImageView.frame, -100);
        [UIView animateWithDuration:0.5 delay:1 options:0 animations:^{
            self.handImageView.alpha = 0;
        } completion:^(BOOL finished) {
            self.handImageView.frame = CGRectMoveByXPixels(self.handImageView.frame, 100);
            [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
                self.handImageView.alpha = 1;
            } completion:^(BOOL finished) {

            }];
        }];
    } completion:^(BOOL finished) {

    }];

推荐答案

问题是使用 UIViewAnimationOptionRepeat 的动画永远不会完成,因此永远不会调用 completion 块.但是,您可以使用 performSelector 方法重复动画,如下面的代码所示.

The problem is that an animation that uses UIViewAnimationOptionRepeat never finishes and therefore never calls the completion block. However, you can repeat the animations using the performSelector method, as shown in the code below.

- (void)repeatingAnimationForView:(UIView *)view
{
    [UIView animateWithDuration:1 delay:0 options:0
    animations:^{ view.frame = CGRectMoveByXPixels(view.frame, -100); }
    completion:^(BOOL finished) {  if ( finished )  {

    [UIView animateWithDuration:0.5 delay:0 options:0
    animations:^{ view.alpha = 0; }
    completion:^(BOOL finished) {  if ( finished )  {

    view.frame = CGRectMoveByXPixels(view.frame, 100);

    [UIView animateWithDuration:0.5 delay:0 options:0
    animations:^{ view.alpha = 1; }
    completion:^(BOOL finished) {  if ( finished )  {

    if ( self.enableRepeatingAnimation )
        [self performSelector:@selector(repeatingAnimationForView:) withObject:view afterDelay:0];

    }}]; }}]; }}];
}

- (void)stopRepeatingAnimationForView:(UIView *)view
{
    self.enableRepeatingAnimation = NO;
    [view.layer removeAllAnimations];
    view.frame = CGRectMake( 100, 137, 80, 50 );
    view.alpha = 1;
}

- (void)startRepeatingAnimationForView:(UIView *)view
{
    self.enableRepeatingAnimation = YES;
    [self repeatingAnimationForView:view];
}

要立即停止动画,请调用 stopRepeatingAnimationForView 方法,如上所示.要在循环结束时停止动画,只需将 self.enableRepeatingAnimation 设置为 NO.

To stop the animations immediately, call the stopRepeatingAnimationForView method as shown above. To stop the animations at the end of a cycle, simply set self.enableRepeatingAnimation to NO.

这篇关于如何使用 UIViewAnimationOptionRepeat 自动重复一组链接的 UIView 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆