I created an empty Cocoa app on Xcode for OS X, and added:
我在Xcode for OS X上创建了一个空的Cocoa应用程序,并添加了:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.view = [[NSView alloc] initWithFrame:NSMakeRect(100, 100, 200, 200)];
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, 1, 1, 1);
[self.window.contentView addSubview:self.view];
}
But the rotated layer's background is clipped by the view's bounding area:
但是旋转图层的背景被视图的边界区域剪切:
I thought since some version of OS X and iOS, the view won't clip the content of its subviews and will show everything inside and outside? On iOS, I do see that behavior, but I wonder why it shows up like that and how to make everything show? (I am already using the most current Xcode 4.4.1 on Mountain Lion).
我想从OS X和iOS的某个版本开始,视图将不会剪切其子视图的内容并显示内部和外部的所有内容?在iOS上,我确实看到了这种行为,但我想知道它为什么会出现这种情况以及如何让一切都显示出来? (我已经在Mountain Lion上使用了最新的Xcode 4.4.1)。
(note: if you try the code above, you will need to link to Quartz Core, and possibly import the quartz core header, although I wonder why I didn't import the header and it still compiles perfectly)
(注意:如果你尝试上面的代码,你将需要链接到Quartz Core,并可能导入石英核心标题,虽然我想知道为什么我没有导入标题并且它仍然完美编译)
1 个解决方案
#1
10
It turns out that if the line:
事实证明,如果该行:
((NSView *)self.window.contentView).wantsLayer = YES;
is added to the very beginning, then it works as expected:
添加到最开始,然后它按预期工作:
- (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);
}
So it looks like if all the views are made to be layer backed, then it works the same as it does on iOS. (If there is a quick way to make all views layer backed automatically, that'd be good).
所以看起来如果所有视图都是层支持的,那么它的工作原理与在iOS上的工作方式相同。 (如果有一种快速方法可以自动备份所有视图层,那就更好了)。
the anchorPoint
line cannot be moved before addSubview
line, or else it is incorrect, although I wonder why that would make any difference.
在addSubview行之前无法移动anchorPoint行,否则它是不正确的,虽然我想知道为什么会有任何区别。
The line self.view.layer = [CALayer layer];
can be removed if window.contentView
is layer backed. Both the contentView and self.view
don't need to set the layer, and I wonder why too.
self.view.layer行[CALayer layer];如果window.contentView是图层备份,则可以删除。 contentView和self.view都不需要设置图层,我也想知道为什么。
The transform
line cannot be before the addSubview
line, or else it won't rotate, and I wonder why too.
变换线不能在addSubview行之前,否则它不会旋转,我也想知道为什么。
The third thing is that, I thought if I go to Interface Builder and make the contentView a class of ContentView
(subclassing NSView
), and in its init
method, do a self.wantsLayer = YES;
, then it would work too, but it does not.
第三件事是,我想如果我去Interface Builder并使contentView成为一个ContentView类(子类化NSView),并在其init方法中,做一个self.wantsLayer = YES;,那么它也会工作,但它才不是。
But anyway, the code above works, and I will update the reasons above why when I find out more.
但无论如何,上面的代码是有效的,当我发现更多时,我会更新上面的原因。
#1
10
It turns out that if the line:
事实证明,如果该行:
((NSView *)self.window.contentView).wantsLayer = YES;
is added to the very beginning, then it works as expected:
添加到最开始,然后它按预期工作:
- (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);
}
So it looks like if all the views are made to be layer backed, then it works the same as it does on iOS. (If there is a quick way to make all views layer backed automatically, that'd be good).
所以看起来如果所有视图都是层支持的,那么它的工作原理与在iOS上的工作方式相同。 (如果有一种快速方法可以自动备份所有视图层,那就更好了)。
the anchorPoint
line cannot be moved before addSubview
line, or else it is incorrect, although I wonder why that would make any difference.
在addSubview行之前无法移动anchorPoint行,否则它是不正确的,虽然我想知道为什么会有任何区别。
The line self.view.layer = [CALayer layer];
can be removed if window.contentView
is layer backed. Both the contentView and self.view
don't need to set the layer, and I wonder why too.
self.view.layer行[CALayer layer];如果window.contentView是图层备份,则可以删除。 contentView和self.view都不需要设置图层,我也想知道为什么。
The transform
line cannot be before the addSubview
line, or else it won't rotate, and I wonder why too.
变换线不能在addSubview行之前,否则它不会旋转,我也想知道为什么。
The third thing is that, I thought if I go to Interface Builder and make the contentView a class of ContentView
(subclassing NSView
), and in its init
method, do a self.wantsLayer = YES;
, then it would work too, but it does not.
第三件事是,我想如果我去Interface Builder并使contentView成为一个ContentView类(子类化NSView),并在其init方法中,做一个self.wantsLayer = YES;,那么它也会工作,但它才不是。
But anyway, the code above works, and I will update the reasons above why when I find out more.
但无论如何,上面的代码是有效的,当我发现更多时,我会更新上面的原因。