为什么 CATransform3DMakeRotation 不将 (0,0,1) 视为绕 Z 轴旋转? [英] Why does a CATransform3DMakeRotation not take (0,0,1) as rotating around the Z-axis?

查看:26
本文介绍了为什么 CATransform3DMakeRotation 不将 (0,0,1) 视为绕 Z 轴旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 Cocoa 的应用程序中转换为 NSView:

If I do a transform to an NSView in Cocoa's app:

self.view.layer.transform = CATransform3DMakeRotation(30 * M_PI / 180, 0, 0, 1);

我看到正方形没有围绕 Z 轴旋转,而是旋转了,就好像该矢量指向下方和向外一样.我需要成功

I see the square not rotated around the Z-axis, but rotated as if that vector is pointing downward and outward. I need to make it

self.view.layer.transform = CATransform3DMakeRotation(30 * M_PI / 180, 1, 1, 1);

使其绕Z轴旋转,如图这个问题.

to make it rotate around the Z-axis, as shown in the picture on this question.

但是,如果我设置一个 NSTimer 然后在 update 方法中更新 transform,然后使用

However, if I set an NSTimer and then update the transform in the update method, then using

-(void) update:(id) info {
    self.view.layer.transform = 
                  CATransform3DMakeRotation(self.degree * M_PI / 180, 0, 0, 1);
    self.degree += 10;
}

(这次,使用 (0, 0, 1))会起作用:它绕 Z 轴旋转.我想知道为什么 (1, 1, 1) 需要在 applicationDidFinishLaunching 内部,但是 (0, 0, 1) 可以在更新方法?

(this time, using (0, 0, 1)) will work: it rotates around the Z-axis. I wonder why (1, 1, 1) is needed inside of applicationDidFinishLaunching , but (0, 0, 1) can be used in the update method?

推荐答案

原来需要先将视图添加到window.contentView:

It turns out the view needs to be added to window.contentView first:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{    
    self.view = [[NSView alloc] initWithFrame:NSMakeRect(100, 100, 200, 200)];

    [self.window.contentView addSubview:self.view];

    self.view.wantsLayer = YES;
    self.view.layer = [CALayer layer];

    self.view.layer.backgroundColor = [[NSColor yellowColor] CGColor];
    self.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
    self.view.layer.transform = CATransform3DMakeRotation(30 * M_PI / 180, 0, 0, 1); 
}

此外,self.view.layer = [CALayer layer]; 行需要在 self.view.wantsLayer = YES; 行之后.为什么这些顺序,我不确定,但它按预期工作.我还找不到提到此订单要求的文档,但上面的代码有效.当我找到更多关于为什么需要订单的信息时,我会更新答案.

also, the line self.view.layer = [CALayer layer]; needs to be after the line self.view.wantsLayer = YES;. Why these order, I am not sure, but it works as it is supposed to. I can't find the docs that mentions the requirement for this order yet, but the code above works. I will update the answer when I find more info about why the order is needed.

这篇关于为什么 CATransform3DMakeRotation 不将 (0,0,1) 视为绕 Z 轴旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆