【问题标题】:Animation repeat动画重复
【发布时间】:2023-03-14 10:00:02
【问题描述】:

由于某种原因,我不得不再次提交相同的动画。

- (void)startAnimation {
    NSLog(@"startAnimation called:shouldContinue = %u",shouldContinue);
    shouldContinue = YES;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:3];
    [UIView setAnimationRepeatCount:10];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop: finished: context:)];
    // bulabula...
    [UIView commitAnimations];
}

- (void)cancelAnimation {
    shouldContinue = NO;
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished    context:(void *)context {
    NSLog(@"animationDidStop called:shouldContinue = %u",shouldContinue);
    if (shouldContinue) {
        [self startAnimation];
    }
}

但结果是,在调用cancelAnimation之前的一轮之后,文本“startAnimation called:shouldContinue = 1”被一次又一次快速打印出来。看起来好像while(YES) { print(,,,); } 出现死循环。

【问题讨论】:

    标签: iphone objective-c animation uiview


    【解决方案1】:

    从动画代码中删除这一行.. [UIView 提交动画]; 它会重复动画...

    【讨论】:

      【解决方案2】:

      请指定该代码

      - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished    context:(void *)context
      {
          NSLog(@"animationDidStop called:shouldContinue = %u",shouldContinue);
      
          if (shouldContinue!=NO) {
              [self startAnimation];
          }
      }
      

      如果没有,请让 shouldContinue to ivar

      如果您将 shouldContinue 设为 ivar,请确保指定

      self.shouldContinue = NO;
      

      而且你没有在任何地方调用 cancelanimation 函数,如果没有,那么在 startanimating 规范的末尾

      self.shouldContinue = NO;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-12
        • 2017-10-06
        • 2016-11-26
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 2013-02-07
        相关资源
        最近更新 更多