【问题标题】:UIView frame animation "wiggle"UIView 帧动画“摆动”
【发布时间】:2012-07-10 19:44:54
【问题描述】:

我有这段疯狂的代码。如何使用 Core Animation 为它制作动画,这样我的代码就更少了?我发现 some code 用于制作“摆动”动画,但那是 3D 的,我只想让视图左右移动,我不想让它在两边弹跳。

[UIView animateWithDuration:0.1f
                 animations:^{
                     CGRect frame = tableView.frame;
                     frame.origin.x -= 4;
                     tableView.frame = frame;
               } completion:^(BOOL finished) {
                   [UIView animateWithDuration:0.1f
                                    animations:^{
                                        CGRect frame = tableView.frame;
                                        frame.origin.x += 4;
                                        tableView.frame = frame;
                                  } completion:^(BOOL finished) {
                                        [UIView animateWithDuration:0.1f
                                                         animations:^{
                                                             CGRect frame = tableView.frame;
                                                             frame.origin.x -= 4;
                                                             tableView.frame = frame;
                                                       } completion:^(BOOL finished) {
                                                             [UIView animateWithDuration:0.1f
                                                                              animations:^{
                                                                                  CGRect frame = tableView.frame;
                                                                                  frame.origin.x = 0;
                                                                                  tableView.frame = frame;
                                                                            } completion:^(BOOL finished) {
                                                                                  // Nothing
                                                                            }
                                                              ];
                                                       }
                                        ];
                                  }
                    ];
               }
];

【问题讨论】:

  • 试试this question's answer,我用过,效果很好。
  • 也不是我想要的。我想要水平来回动画视图的位置,我不希望它摆动/摆动。不过谢谢!
  • 你可以使用类似的方法,只需将变换从旋转更改为平移
  • 当然!多哈。您应该发布作为答案,我会接受:)
  • 您不想让它摆动吗?你可能想改变问题的标题。

标签: ios core-animation uiviewanimation


【解决方案1】:

我想跟进这个问题,以防有人偶然发现它。我现在正在使用关键帧:

-(void)wiggleView {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position.x";
    animation.values = @[ @0, @8, @-8, @4, @0 ];
    animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];
    animation.duration = 0.4;
    animation.additive = YES;
    [self.layer addAnimation:animation forKey:@"wiggle"];
}

【讨论】:

  • 我要如何再次停止这样的动画?
  • @meaning-matters 使用 [view.layer removeAllAnimations];
【解决方案2】:

This question 是一种执行重复运动动画的非常简洁的方式。我将它用于旋转摆动,但您可以应用任何转换,在您的情况下,是平移

【讨论】:

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