My CALayer transform holds after animation, but the perspective disappears

余生长醉 提交于 2019-12-10 14:14:54

问题


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:

At the end:

What am I doing wrong?


回答1:


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.




回答2:


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 :



来源:https://stackoverflow.com/questions/16113417/my-calayer-transform-holds-after-animation-but-the-perspective-disappears

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!