Iphone 使用CATTransferM3dMakeRotation时无透视图

Iphone 使用CATTransferM3dMakeRotation时无透视图,iphone,ios,objective-c,core-animation,catransform3drotate,Iphone,Ios,Objective C,Core Animation,Catransform3drotate,我正在尝试使用CATTransferM3DMakeRotation在UIView层上执行3D旋转。我不明白为什么这个简单的例子没有显示透视图,而是看起来像被压扁了一样。使用CATTransferM3dRotate时,我可以使旋转正常工作,但我不想旋转,我想旋转到。谢谢 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

我正在尝试使用
CATTransferM3DMakeRotation
在UIView层上执行3D旋转。我不明白为什么这个简单的例子没有显示透视图,而是看起来像被压扁了一样。使用
CATTransferM3dRotate
时,我可以使旋转正常工作,但我不想旋转,我想旋转到。谢谢

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [newView setBackgroundColor:[UIColor orangeColor]];
    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 20)];
    [newLabel setText:@"Hello"];
    [newView addSubview:newLabel];
    [self.window addSubview:newView];

    CALayer *layer = newView.layer;

    CATransform3D perspective = CATransform3DMakeRotation(1.0, 0, 1, 0);
    perspective.m34 = -1 / 500;

    layer.anchorPoint = CGPointMake(1.0, 0.5);
    layer.zPosition = 99;
    layer.transform = perspective;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
下面是它的屏幕截图:

David回答后更新: 为了加强David关于顺序重要的观点,我想看看矩阵之间的差异,并对其进行编码:

CATransform3D transform1 = CATransform3DMakeRotation(-1, 0, 1, 0);
transform1.m34 = -1.0 / 500.0;

NSLog(@"%f %f %f %f", transform1.m11, transform1.m12, transform1.m13, transform1.m14);
NSLog(@"%f %f %f %f", transform1.m21, transform1.m22, transform1.m23, transform1.m24);
NSLog(@"%f %f %f %f", transform1.m31, transform1.m32, transform1.m33, transform1.m34);
NSLog(@"%f %f %f %f", transform1.m41, transform1.m42, transform1.m43, transform1.m44);

NSLog(@"==============================");

CATransform3D transform2 = CATransform3DIdentity;
transform2.m34 = -1.0 / 500.0;
transform2 = CATransform3DRotate(transform2, -1, 0, 1, 0);

NSLog(@"%f %f %f %f", transform2.m11, transform2.m12, transform2.m13, transform2.m14);
NSLog(@"%f %f %f %f", transform2.m21, transform2.m22, transform2.m23, transform2.m24);
NSLog(@"%f %f %f %f", transform2.m31, transform2.m32, transform2.m33, transform2.m34);
NSLog(@"%f %f %f %f", transform2.m41, transform2.m42, transform2.m43, transform2.m44);

NSLog(@"==============================");

CATransform3D transform3 = CATransform3DIdentity;
transform3 = CATransform3DRotate(transform3, -1, 0, 1, 0);
transform3.m34 = -1.0 / 500.0;

NSLog(@"%f %f %f %f", transform3.m11, transform3.m12, transform3.m13, transform3.m14);
NSLog(@"%f %f %f %f", transform3.m21, transform3.m22, transform3.m23, transform3.m24);
NSLog(@"%f %f %f %f", transform3.m31, transform3.m32, transform3.m33, transform3.m34);
NSLog(@"%f %f %f %f", transform3.m41, transform3.m42, transform3.m43, transform3.m44);
这将产生以下输出:

0.540302 0.000000 0.841471 0.000000
0.000000 1.000000 0.000000 0.000000
-0.841471 0.000000 0.540302 -0.002000
0.000000 0.000000 0.000000 1.000000
==============================
0.540302 0.000000 0.841471 -0.001683
0.000000 1.000000 0.000000 0.000000
-0.841471 0.000000 0.540302 -0.001081
0.000000 0.000000 0.000000 1.000000
==============================
0.540302 0.000000 0.841471 0.000000
0.000000 1.000000 0.000000 0.000000
-0.841471 0.000000 0.540302 -0.002000
0.000000 0.000000 0.000000 1.000000

我总是发现m34属性很挑剔,请尝试以下方法:

CATTransferM3D transform=CATTransferorM3D实体


而不是
CATTransferM3dMakeRotation(1.0,0,1,0)

设置
m34
值并不能为您提供所有magic变换的透视图(不幸的是)。由于旋转会更改变换的许多不同值,因此需要在标识消息上设置透视图,然后旋转该变换

CATransform3D rotationWithPerspective = CATransform3DIdentity;
rotationWithPerspective.m34 = -1.0/500.0;
rotationWithPerspective = CATransform3DRotate(rotationWithPerspective, angle, 0, 1, 0);
这里与您所做的不同之处在于,您创建了一个身份矩阵,并在其上设置透视图,然后旋转该矩阵。问题是秩序很重要



如果您了解矩阵数学,那么您可以验证这两种变换之间的差异,并了解为什么您的变换没有提供任何透视图。如果你有兴趣学习我写的转换背后的数学(警告:它很长,只包含数学)

是否可以将.m34设置为
-1/500
作为整数除法?尝试使用
-1.0/500.0
@ZevEisenberg谢谢你的建议,但是没有用。谢谢你的回答和伟大的帖子@davidronqvist。非常有帮助。@david哦,要是我知道顺序很重要就好了)非常感谢!