事实证明,如果该行:
((NSView *)self.window.contentView).wantsLayer = YES;
添加到最开始,然后它按预期工作:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
((NSView *)self.window.contentView).wantsLayer = YES;
self.view = [[NSView alloc] initWithFrame:NSMakeRect(200, 200, 200, 200)];
self.view.wantsLayer = YES;
self.view.layer.backgroundColor = [[NSColor yellowColor] CGColor];
[self.window.contentView addSubview:self.view];
self.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
self.view.layer.transform = CATransform3DMakeRotation(30 * M_PI / 180, 0, 0, 1);
}
因此,看起来如果所有视图都支持图层,那么它的工作方式与 iOS 上的工作方式相同。 (如果有一种快速方法可以自动支持所有视图图层,那就太好了)。
the anchorPoint
之前不能移动线addSubview
线,否则它是不正确的,尽管我想知道为什么这会产生任何影响。
线路self.view.layer = [CALayer layer];
可以删除,如果window.contentView
是层支持的。 contentView 和self.view
不需要设置图层,我也想知道为什么。
The transform
该行不能位于addSubview
线,否则它不会旋转,我也想知道为什么。
第三件事是,我想如果我去 Interface Builder 并将 contentView 设为一个类ContentView
(子类化NSView
),并在其init
方法,做一个self.wantsLayer = YES;
,那么它也可以工作,但事实并非如此。
但无论如何,上面的代码是有效的,当我了解更多信息时,我会更新上面的原因。