开发者

Why won't my layer animate about the Z axis?

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, 开发者_JAVA百科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;
}


Your problem appears to be that you are trying to use a 360 degree transform to animate your layer. To Core Animation, a layer transformed by 360 degrees is exactly the same as the starting layer, so no animation is performed.

What you will want to do as animate around the transform.rotation.z keypath, as described in this answer. This is a helper keypath that Core Animation provides, and because your starting and ending values (the angles through which to rotate) are different, Core Animation doesn't do the same sort of optimization you see with transforms.


You need to apply a perspective transform to your layer before animating it like this, otherwise the rotation your'e doing will not look right.

CATransform3D transform = CATransform3DIdentity; transform.m34 = 1.0 / -2000;

set the transform on the layer to that, and then animate like you are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜