macos - 为什么 CATransform3DMakeRotation 不将 (0,0,1) 视为绕 Z 轴旋转?

标签 macos cocoa appkit catransform3d

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

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

我看到正方形没有绕 Z 轴旋转,而是旋转得好像该向量指向向下和向外。我需要做到

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

使其绕Z轴旋转,如this question上的图片所示.

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

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

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

最佳答案

事实证明,需要先将 View 添加到 window.contentView 中:

- (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; 行之后。为什么这些顺序,我不确定,但它按预期工作。我还找不到提到此订单要求的文档,但上面的代码可以工作。当我找到有关为什么需要订单的更多信息时,我会更新答案。

关于macos - 为什么 CATransform3DMakeRotation 不将 (0,0,1) 视为绕 Z 轴旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12400474/

相关文章:

eclipse - 我的Mac上没有.bash_profile

MacOS Kitematic 如何配置卷

Mac OS X Lion 上的 Java 1.6.0_26

iphone - 如何从 NSManagedObjectContextObjectsDidChangeNotification 中排除属性

cocoa - 在项目中包含自定义框架时出现问题

objective-c - 如何在 NSVisualEffectView 上叠加背景色并使用鲜艳的控件?

macos - 在swift中添加通知观察者

cocoa - 错误 "Create a concrete instance!"

cocoa - 如何在 Mac OS X 上绘制桌面?

macos - 如何检测在 NSPressGestureRecognizer 事件期间是否按下了 CMD 或 Shift 键?