ConfusedNoob
ConfusedNoob

Reputation: 10186

My CALayer transform holds after animation, but the perspective disappears

I have the following code that rotates a CALayer by -45degrees on the Y axis:

#define D2R(x) (x * (M_PI/180.0))

- (void) swipe:(UISwipeGestureRecognizer *)recognizer
{        
    CATransform3D transform = CATransform3DMakeRotation(D2R(-45), 0, 1.0, 0);
    transform.m34 = -1.0 / 850;

    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: @"transform"];
    transformAnimation.fillMode = kCAFillModeForwards;
    transformAnimation.removedOnCompletion = NO;
    transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];
    transformAnimation.duration = 0.5;

    [self.layer addAnimation:transformAnimation forKey:@"transform"];
}

The animation works, except it ends with no perspective - ignoring my m34 setting if I'm understanding things correctly.

Halfway through:

enter image description here

At the end:

enter image description here

What am I doing wrong?

Upvotes: 6

Views: 3657

Answers (2)

Guo Luchuan
Guo Luchuan

Reputation: 4731

Try this :

- (void) swipe:(UISwipeGestureRecognizer *)recognizer
{        

    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -10 / 850.0;
    transform = CATransform3DRotate(transform, D2R(-45), 0, 1.0, 0);

    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: @"transform"];
    transformAnimation.fillMode = kCAFillModeForwards;
    transformAnimation.removedOnCompletion = NO;
    transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];
    transformAnimation.duration = 0.5;

    [self.layer addAnimation:transformAnimation forKey:@"transform"];
}

And end the effect is like this :

enter image description here

Upvotes: 0

Dave Batton
Dave Batton

Reputation: 8845

Animation only affects the appearance of the view during the animation. It doesn't get applied to the view after the animation ends. You need to do this yourself. I'm guessing something like this right after adding the animation will work:

self.layer.transform = transform;

You can do this right away, as the animation will hide it until the animation completes.

Upvotes: 2

Related Questions