I have an image which I place in a CALayer, then add that layer as a sublayer of the main view. When I try to animate it around its Z axis, all I see is a static image.
Why won't my layer animate? The relevant code is as follows:
- (void)viewDidLoad {
UIImage *myImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"about" ofType:@"png"]];
earthLayer = [CALayer layer];
[earthLayer setContents:(id)[myImage CGImage]];
[earthLayer setBounds:CGRectMake(240,360, 40, 200)];
[earthLayer setPosition:CGPointMake(280, 280)];
[earthLayer setName:@"earth"];
[earthLayer addAnimation:[self animationForSpinning] forKey:@"Rahul"];//here animatin is being added to the layer..
[[self.view layer] addSublayer:earthLayer];
//[self spinLayer:earthLayer];
}
//the animation method which returns a CAAnimation.
- (CAAnimation*)animationForSpinning {
// Create a transform to rotate in the z-axis
float radians = DegreeToRadian( 360 );
CATransform3D transform;
transform = CATransform3DMakeRotation(radians, 0, 1.0,0, 1.0);
// Create a basic animation to animate the layer's transform
CABasicAnimation* animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
// Now assign the transform as the animation's value. While
// animating, CABasicAnimation will vary the transform
// attribute of its target, which for this transform will spin
// the target like a wheel on its z-axis.
animation.toValue = [NSValue valueWithCATransform3D:transform];
animation.duration = 2; // two seconds
animation.cumulative = YES;
animation.repeatCount = 10000;// "forever"
return animation;
}