Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView frame animation "wiggle"

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
                                                                            }
                                                              ];
                                                       }
                                        ];
                                  }
                    ];
               }
];
like image 531
runmad Avatar asked Feb 19 '23 21:02

runmad


1 Answers

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"];
}
like image 108
runmad Avatar answered Mar 04 '23 08:03

runmad