4
votes

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
                                                                            }
                                                              ];
                                                       }
                                        ];
                                  }
                    ];
               }
];
2
Try this question's answer, I used it and it works great.Dan F
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
You could use a similar method though, just change the transform from a rotation to a translationDan F
Of course! Doh. You should post as an answer and I will accept :)runmad
you dont want it to wiggle? you might want to change the question title then.scord

2 Answers

8
votes

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

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