如何使NSView不剪切其边界区域? [英] How to make NSView not clip its bounding area?

查看:182
本文介绍了如何使NSView不剪切其边界区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode为OS X创建了一个空Cocoa应用程序,并添加:

I created an empty Cocoa app on Xcode for OS X, and added:

- (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:

我认为自从某些版本的OS X和iOS,视图将不会剪辑其子视图的内容,将显示内部和外部的一切?在iOS上,我确实看到了这种行为,但我不知道为什么会出现这样的情况,如何使一切显示? (我已经使用最新的Xcode 4.4.1在山狮)。

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).

(注意:如果你尝试上面的代码,你需要链接到Quartz Core,并且可能导入石英核心头,虽然我不知道为什么我没有导入标题,它仍然完美编译)

(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)

推荐答案

原来,如果行:

((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);

}

所以看起来像是所有的视图都是层支持,那么它的工作原理与它在iOS上的相同。 (如果有一个快速的方法使所有的视图层自动地支持,那将是很好的)。

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).

anchorPoint 行不能在 addSubview 行之前移动,否则它是不正确的,虽然我不知道为什么会有什么区别。

the anchorPoint line cannot be moved before addSubview line, or else it is incorrect, although I wonder why that would make any difference.

如果 window.contentView ,那么 self.view.layer = [CALayer layer]; 是层支持。 contentView和 self.view 都不需要设置图层,我也不知道为什么。

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.

transform 行不能在 addSubview 行之前,否则它不会旋转,我也想知道为什么。

The transform line cannot be before the addSubview line, or else it won't rotate, and I wonder why too.

第三件事是,我想如果我去Interface Builder,让contentView成为 ContentView (子类化 NSView ),并在其 init 方法中,执行 self.wantsLayer = YES ;

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.

但是无论如何,上面的代码工作,我将更新上面的原因为什么我会找到更多。

But anyway, the code above works, and I will update the reasons above why when I find out more.

这篇关于如何使NSView不剪切其边界区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆