In my xcode project I have an UIImage. I want to make it shaking (animation) from left to right a little bit. I wrote this code, however, its not working.
-(void)errorShake
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(-20 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(20 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)];
[lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)];
[UIView commitAnimations];
}
Could you tell me please, how can I make my image shake ?
Here’s the code I use for that effect.
As
Jerementioned. Make sure to include the import:You can also add it as a category on
UIView.