如果我在 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/