Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALayer with rotation animation

I have masked an image like this:

UIView *maskImage; maskImage = [[UIView alloc] init];
maskImage.backgroundColor = UIColorFromRGB(FTRMaskColor);
maskImage.frame = newFrame;

CALayer *theLayer = [CALayer layer];
theLayer.contents = (id)[[UIImage imageNamed:@"image.png"] CGImage];
theLayer.frame = newFrame;

maskImage.layer.mask = theLayer;

It works fine but the main problem is if I want to rotate the my Ipad, the rotation animation of the view or the layer (I'm not very sure) doesn't work. It rotates without animation. Could you help, please?

like image 430
Alex Avatar asked Jul 14 '11 08:07

Alex


1 Answers

To rotate a CALayer:

NSNumber *rotationAtStart = [myLayer valueForKeyPath:@"transform.rotation"];
CATransform3D myRotationTransform = CATransform3DRotate(myLayer.transform, myRotationAngle, 0.0, 0.0, 1.0);
myLayer.transform = myRotationTransform;        
CABasicAnimation *myAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
myAnimation.duration = kMyAnimationDuration;
myAnimation.fromValue = rotationAtStart;
myAnimation.toValue = [NSNumber numberWithFloat:([rotationAtStart floatValue] + myRotationAngle)];
[myLayer addAnimation:myAnimation forKey:@"transform.rotation"];

myRotationAngle should be in radians. Use negative values for counter-clockwise rotation and positive values for clockwise.

like image 95
Spiros Avatar answered Nov 08 '22 23:11

Spiros