UIView帧动画"摆动"

run*_*mad 4 core-animation uiviewanimation ios

我有这段疯狂的代码.如何使用Core Animation为此设置动画,以便减少代码?我已经找到了一些用于做"摆动"动画的代码,但这是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
                                                                            }
                                                              ];
                                                       }
                                        ];
                                  }
                    ];
               }
];
Run Code Online (Sandbox Code Playgroud)

run*_*mad 8

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

-(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"];
}
Run Code Online (Sandbox Code Playgroud)