How to make NSView not clip its bounding area?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 03:02:30

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

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

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!