CATransform3D 透视图不适用于 CATransformLayer

CATransform3D perspective not working on CATransformLayer

注意:请忽略颜色、渐变和大小!我担心的是视角没有按预期工作

我正在尝试实现如下输出,

但是当我 运行 我的代码时,它给我以下输出

请看一下我的代码:

CATransformLayer *baseLayer = [CATransformLayer layer];
baseLayer.anchorPoint = CGPointZero;
baseLayer.bounds = self.view.bounds;
baseLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

[self.view.layer addSublayer:baseLayer];

CALayer *redLayer = [CALayer layer];
redLayer.backgroundColor = [UIColor redColor].CGColor;
redLayer.frame = CGRectMake(0, 0, 100, 100);
redLayer.position = CGPointMake(0,0);
redLayer.anchorPoint = CGPointMake(1, 0.5); // right
[baseLayer addSublayer:redLayer];

CALayer *blueLayer = [CALayer layer];
blueLayer.backgroundColor = [UIColor blueColor].CGColor;
blueLayer.bounds = CGRectMake(0, 0, 600, 100);
blueLayer.anchorPoint = CGPointMake(1, 0.5); // right
blueLayer.position = CGPointMake(-50,0);
[baseLayer addSublayer:blueLayer];

CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1/80;
transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0);
blueLayer.transform = transform;

baseLayer.sublayerTransform = CATransform3DRotate(CATransform3DIdentity, degreesToRadians(80), 0, 1, 0);

我不知道是什么让这个问题得到解决,但这是我更新后的代码,它按预期工作。

CATransformLayer *baseLayer = [CATransformLayer layer];
baseLayer.anchorPoint = CGPointZero;
baseLayer.bounds = self.view.bounds;
baseLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

CALayer *redLayer = [CALayer layer];
redLayer.backgroundColor = [UIColor redColor].CGColor;
redLayer.frame = CGRectMake(0, 0, 1000, 250);
redLayer.position = CGPointMake(0,0);
redLayer.anchorPoint = CGPointMake(0, 0.5); // right
redLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI_2,0.0f, 1.0f, 0.0f);
[baseLayer addSublayer:redLayer];

CALayer *blueLayer = [CALayer layer];
blueLayer.backgroundColor = [UIColor blueColor].CGColor;
blueLayer.bounds = CGRectMake(0, 0, 125, 250);
blueLayer.anchorPoint = CGPointMake(0, 0.5); // right
blueLayer.position = CGPointMake(0,0);
[baseLayer addSublayer:blueLayer];

CGFloat perspective = -1000.0; //This relates to the m34 perspective matrix.

CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / perspective;
rotationAndPerspectiveTransform = CATransform3DRotate(newTransform, degreesToRadians(10) , 0.0f, 1.0f, 0.0f);


baseLayer.sublayerTransform = rotationAndPerspectiveTransform;