4

I have this insane piece of code. How do I animate this with Core Animation so I have way less code? I have found some code for doing "wiggle" animation but that's in 3D, I just want the view to move side to side, I don't want to animation it bouncing on the sides.

[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
                                                                            }
                                                              ];
                                                       }
                                        ];
                                  }
                    ];
               }
];
Community
  • 1
  • 1
runmad
  • 14,846
  • 9
  • 99
  • 140
  • Try [this question's answer](http://stackoverflow.com/questions/929364/how-to-create-iphones-wobbling-icon-effect), I used it and it works great. – Dan F Jul 10 '12 at 19:46
  • Not what I am going for either. I *only* want to animate the view's position back and forth horizontally, I don't want it to wiggle/wobble. Thanks, though! – runmad Jul 10 '12 at 19:50
  • You could use a similar method though, just change the transform from a rotation to a translation – Dan F Jul 10 '12 at 19:51
  • Of course! Doh. You should post as an answer and I will accept :) – runmad Jul 10 '12 at 19:52
  • you dont want it to wiggle? you might want to change the question title then. – scord Jul 10 '12 at 21:34

2 Answers2

9

I wanted to follow up with this question in case anyone stumbles onto it. I'm now using key frames:

-(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"];
}
runmad
  • 14,846
  • 9
  • 99
  • 140
0

This question is a very concise way of performing a repeated motion animation. I used it for a rotation wiggle, but you can apply any transformation, in your case, a translation

Community
  • 1
  • 1
Dan F
  • 17,654
  • 5
  • 72
  • 110