将层支持的视图与非层支持的视图混合,导致超出边界的怪异窗口角

时间:2019-01-25 17:27:30

标签: objective-c cocoa calayer nsview

因此,可悲的是,这是很难提供MVCE的情况之一,但我会尽力描述所有细节。

所以我有一个像这样的以编程方式构造的NSWindow-

_window = [[NSWindow alloc] initWithContentRect:_frameRect styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskTexturedBackground backing:NSBackingStoreBuffered defer:NO];
if ([_window respondsToSelector:@selector(titlebarAppearsTransparent)])
{
   _window.titlebarAppearsTransparent = true;
}
CGFloat x = (_window.screen.frame.size.width - _window.frame.size.width) / 2, y = (_window.screen.frame.size.height - _window.frame.size.height) / 2;
[_window setFrameOrigin:NSMakePoint(x, y)];
[[_window standardWindowButton:NSWindowZoomButton] setHidden: YES];
[[_window standardWindowButton:NSWindowMiniaturizeButton] setHidden: YES];
_window.backgroundColor = [NSColor colorWithHexColorString:@"f4f7fc"];

然后,我通过将不同的contentView派生类设置为内容视图,以编程方式更改NSView。我总是将构造窗口时所使用的_frameRect传递给他们。

一切都很好,直到我添加了一个使用关键帧动画的类。我必须添加一个图层,现在-在该视图之后设置为contentView的任何视图中,都有奇怪的黑色角从圆角窗口角出来。错误的视图是这样创建的:

-(instancetype)initWithFrame:(NSRect)frameRect
{
   if (self = [super initWithFrame:frameRect])
   {
       self.wantsLayer = YES;
       _spinnerImageView = [[NSImageView alloc] initWithFrame:NSMakeRect(348, 342, 24, 24)];
       _spinnerImageView.wantsLayer = YES;
       [_spinnerImageView setLayer:[CALayer layer]];
       NSMutableArray<NSImage*>* images = [NSMutableArray arrayWithCapacity:30];
       for (NSUInteger spinnerImageIndex = 0; spinnerImageIndex < 30; ++spinnerImageIndex)
       {
           NSString* image = [NSString stringWithFormat:@“frame-%@“, @(spinnerImageIndex)];
           [images addObject:[NSImage imageNamed:image]];
       }
       _spinnerAnimation = [CAKeyframeAnimation animationWithKeyPath:kAnimationKey];
       [_spinnerAnimation setCalculationMode:kCAAnimationDiscrete];
       [_spinnerAnimation setDuration:0.75f];
       [_spinnerAnimation setRepeatCount:HUGE_VALF];
       [_spinnerAnimation setValues:images];
       [self addSubview:_spinnerImageView];
       _pleaseWaitText = [NSAttributedString viewTitleWithText:@“Please wait...“];
       [self resetToInitialState];
       _loadingButton = [[DisabledButton alloc] initWithFrame:NSMakeRect(288, 16, 144, 32) andText:@“Please wait...“];
       [self addSubview:_loadingButton];
   }
   return self;
}

所以发生的是,带有动画的屏幕看起来不错(注意角落): Window with correct corners 从该视图导航到没有将wantsLayer设置为YES的新视图后,我得到了损坏的角落: enter image description here 这只是一个示例,但是从带有动画的视图导航到其他视图基本上会导致黑色边框。

现在,我尝试阅读层系统,但是我无法查明我到底在做什么错。我以为混合非层支持的视图和层支持的视图会搞砸,但是当将所有视图的wantsLayer设置为YES时,会得到不同的工件。我真的希望黑角的东西会触发可可豆经验丰富的人的“哦,这是由”反射引起的。

0 个答案:

没有答案
相关问题