Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
I have a UIView called qShake. When the user makes an error, I want that view to shake from left to right in a small space for a short period of time. How would I create an animation to do that?
You can use a CABasicAnimation, and set the repeat count to some small value, while animating the position of the view.layer that you want to vibrate. Note that clipsToBounds
for the view must be set to NO. Also be sure to include the QuartzCore framework in the project.
- (void)shakeView:(UIView *)view
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
animation.duration = 0.1;
animation.byValue = @(20);
animation.autoreverses = YES;
animation.repeatCount = 10;
[view.layer addAnimation:animation forKey:@"Shake"];
}