2012-07-10 79 views
4

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

[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 
                      } 
                   ]; 
                 } 
             ]; 
            } 
        ]; 
       } 
]; 
+0

尝试[这个问题的回答(http://stackoverflow.com/questions/929364/how-to-create-iphones-wobbling-icon-effect),我用它和它很好。 – 2012-07-10 19:46:48

+0

不是我要做的。我*只*想要水平来回激活视图的位置,我不想让它晃动/摆动。不过谢谢! – runmad 2012-07-10 19:50:34

+0

虽然你可以使用类似的方法,只需要改变从旋转到翻译的转换 – 2012-07-10 19:51:28

回答

0

This question是执行重复运动动画的非常简明的方式。我用它来旋转摆动,但你可以应用任何转换,在你的情况下,翻译

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"]; 
} 
+0

我将如何停止这样的动画? – 2016-12-03 14:59:39

+0

@意义事项使用[view.layer removeAllAnimations]; – 2017-03-21 09:45:07