1

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

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轴旋转,如本题图片所示。

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

-(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)需要 inside applicationDidFinishLaunching,但(0, 0, 1)可以在update方法中使用?

4

1 回答 1

2

事实证明,需要首先添加视图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;。为什么这些订单,我不确定,但它按预期工作。我找不到提到此订单要求的文档,但上面的代码有效。当我找到有关为什么需要订单的更多信息时,我会更新答案。

于 2012-09-15T04:42:00.083 回答