macOS NSView改变frame后会出现黑色残留,应付的办法是不在drawRect上修改重新initWithFrame一下就行
黑色部分就是残留。是因为绘制后保留了轨迹。
解决办法是不在drawRect中做处理重新写NSView,新增方法 initWithFrame
- (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; //cocoa方法的改变背景颜色 [[NSColor redColor] setFill]; NSRectFill(dirtyRect); // Drawing code here. // CGFloat lineWidth = 2.0; // // Drawing code here. // NSBezierPath* bgPath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(dirtyRect, 10.0f, 10.0f) xRadius:3.0 yRadius:3.0]; // [bgPath setLineWidth:lineWidth]; // [bgPath setClip]; // [[NSColor purpleColor] setFill]; // [bgPath fill]; } - (instancetype)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { [self setWantsLayer:YES]; self.layer.backgroundColor = CGColorCreateGenericRGB(1.0, 0, 0, 1.0); // NSShadow *shadow = [[NSShadow alloc] init]; // [shadow setShadowColor: [NSColor redColor]]; // [shadow setShadowBlurRadius:4.0]; // [self setShadow:shadow]; // [self addSubview:self.klineVC.view]; // [self.klineVC.view mas_makeConstraints:^(MASConstraintMaker *make) { // (void)make.edges; // }]; } return self; }
效果如图所示