Wednesday, February 15, 2012

How to create wiggle animation for your UIView ?

So you wondering about how to create the world famous wiggling action in your app.

Wiggling action is what happens when you long press on icons on your iPhone/iPad screen and they start dancing. Here is the code:

   CATransform3D transform = CATransform3DMakeRotation(0.08, 0, 0, 1.0);  
   CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];  
   animation.toValue = [NSValue valueWithCATransform3D:transform];  
   animation.autoreverses = YES;   
   animation.duration = 0.1;   
   animation.repeatCount = 10000;   
   animation.delegate=self;  
   [[self layer] addAnimation:animation forKey:@"wiggleAnimation"];  



1 comment:

  1. Thanks! This is just what I was looking for.

    Here's a Swift version of the same code:

    var transform:CATransform3D = CATransform3DMakeRotation(0.08, 0, 0, 1.0);
    var animation:CABasicAnimation = CABasicAnimation(keyPath: "transform");
    animation.toValue = NSValue(CATransform3D: transform);
    animation.autoreverses = true;
    animation.duration = 0.5;
    animation.repeatCount = 10000;
    animation.delegate=self;
    myView.layer.addAnimation(animation, forKey: "wiggleAnimation");

    ReplyDelete