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

查看:24
本文介绍了如何使 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 上,我确实看到了这种行为,但我想知道为什么它会这样显示以及如何让所有内容都显示出来?(我已经在 Mountain Lion 上使用了最新的 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.

self.view.layer = [CALayer layer]; 如果 window.contentView 是层支持的,则可以删除.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天全站免登陆