how to create iphone's wobbling icon effect? how to create iphone's wobbling icon effect? ios ios

how to create iphone's wobbling icon effect?


Ramin's answer was very good, but since OS4 the same effect can be achieved using animateWithDuration in one simple function too.

(adapted his example for future googlers)

#define RADIANS(degrees) (((degrees) * M_PI) / 180.0)- (void)startWobble { itemView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5)); [UIView animateWithDuration:0.25       delay:0.0       options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse)      animations:^ {       itemView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5));      }      completion:NULL ];}- (void)stopWobble { [UIView animateWithDuration:0.25      delay:0.0       options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear)      animations:^ {       itemView.transform = CGAffineTransformIdentity;      }      completion:NULL  ];}


Simple way to do it:

#define RADIANS(degrees) (((degrees) * M_PI) / 180.0)CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5.0));CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5.0));itemView.transform = leftWobble;  // starting point[UIView beginAnimations:@"wobble" context:itemView];[UIView setAnimationRepeatAutoreverses:YES]; // important[UIView setAnimationRepeatCount:10];[UIView setAnimationDuration:0.25];[UIView setAnimationDelegate:self];[UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)];itemView.transform = rightWobble; // end here & auto-reverse[UIView commitAnimations];...- (void) wobbleEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {     if ([finished boolValue]) {        UIView* item = (UIView *)context;        item.transform = CGAffineTransformIdentity;     }}

Probably have to play with timing and angles but this should get you started.

EDIT: I edited the response to add code to put the item back in its original state when done. Also, note that you can use the beginAnimations context value to pass along anything to the start/stop methods. In this case it's the wobbling object itself so you don't have to rely on specific ivars and the method can be used for any generic UIView-based object (i.e. text labels, images, etc.)


You should use CAKeyframeAnimation to make a smoother animation.

+ (void) animationKeyFramed: (CALayer *) layer                    delegate: (id) object              forKey: (NSString *) key {    CAKeyframeAnimation *animation;    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];    animation.duration = 0.4;    animation.cumulative = YES;    animation.repeatCount = 2;    animation.values = [NSArray arrayWithObjects:            [NSNumber numberWithFloat: 0.0],             [NSNumber numberWithFloat: RADIANS(-9.0)],             [NSNumber numberWithFloat: 0.0],            [NSNumber numberWithFloat: RADIANS(9.0)],            [NSNumber numberWithFloat: 0.0], nil];    animation.fillMode = kCAFillModeForwards;    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];    animation.removedOnCompletion = NO;    animation.delegate = object;    [layer addAnimation:animation forKey:key];}