【问题标题】:How to repeat shake animation indefinitely or X times using animateWithDuration?如何使用 animateWithDuration 无限期或 X 次重复摇动动画?
【发布时间】:2016-08-05 08:28:15
【问题描述】:

下面的代码来自这个 SO 问题:UIView shake animation

这个抖动动画非常完美,只是它在一次迭代后停止。如何无限期地重复动画,或者如何让它重复 X 次?

关于重复 UIView 动画还有其他 SO 答案建议使用 CAKeyframeAnimationCABasicAnimation,但这个问题是不同的。目标是重现这个精确的动画,具有来自usingSpringWithDampinginitialSpringVelocity. 的“弹性”效果

使用 Autoreverse 和 Repeat 不会重现所需的效果,因为初始平移位于动画块之外。

view.transform = CGAffineTransformMakeTranslation(20, 0);
[UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.2 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    view.transform = CGAffineTransformIdentity;
} completion:nil];

【问题讨论】:

    标签: ios animation core-animation uiviewanimation


    【解决方案1】:

    如果您需要几次完全相同的代码而不是将该代码放入方法中并进行递归,则类似这样:

    - (void)animateCount:(NSInteger)count {
       if (count == 0) {
          return;
       }
    
       view.transform = CGAffineTransformMakeTranslation(20, 0);
       [UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.2 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
          view.transform = CGAffineTransformIdentity;
       } completion:^{
          count--;
          [self animateCount:count];
       }];
    }
    

    【讨论】:

      【解决方案2】:

      在选项中使用(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) 无限期地重复它

      [UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.2 initialSpringVelocity:1.0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut) animations:^{
      
          someView.frame = someFrame1;
      
      } completion:^(BOOL finished) {
      
          [UIView animateWithDuration:0.5f animations:^{
      
              someView.frame = someFrame2;
      
          } completion:nil];
      
      }]; 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多