My CALayer transform is done after the animation, but the perspective disappears

I have the following code that rotates CALayer -45 degrees 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 that it ends without perspective - ignoring the m34 setting if I understand things correctly.

Halfway through:

enter image description here

In the end:

enter image description here

What am I doing wrong?

+6
source share
2 answers

Animation only affects the appearance of the view during the animation. It does not apply to the view after the animation ends. You need to do it yourself. I guess something similar right after adding the animation:

 self.layer.transform = transform; 

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

+2
source

Try the following:

 - (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 the end of the effect will be like this:

enter image description here

+1
source

All Articles