gpt4 book ai didi

macos - 如何使用不透明窗口创建内部透明 nsview

转载 作者:行者123 更新时间:2023-12-03 17:47:17 26 4
gpt4 key购买 nike

我正在尝试在非不透明窗口中创建不透明边框。目前,我寻找 subview 并创建贝塞尔路径来填充外部区域。还有其他方法可以做到这一点吗?

举例说明我想要实现的目标:

enter image description here

#import "MyView2.h"
#import "MyView.h"

@interface MyView2() {
NSRect transparentSubviewRect;
}

@end

@implementation MyView2

- (void)awakeFromNib
{
NSArray *subviews = [self subviews];
for (NSView *view in subviews) {
if ([view isKindOfClass:[MyView class]]) {
transparentSubviewRect = [view frame];
}
}
}

- (void)drawRect:(NSRect)dirtyRect {
NSRect rect = [self frame];
NSBezierPath *path = [NSBezierPath bezierPath];
[path moveToPoint:NSMakePoint(NSMaxX(transparentSubviewRect), NSMaxY(transparentSubviewRect))];
[path lineToPoint:NSMakePoint(NSMinX(transparentSubviewRect), NSMaxY(transparentSubviewRect))];
[path lineToPoint:NSMakePoint(NSMinX(transparentSubviewRect), NSMinY(transparentSubviewRect))];
[path lineToPoint:NSMakePoint(NSMaxX(transparentSubviewRect), NSMinY(transparentSubviewRect))];
[path lineToPoint:NSMakePoint(NSMaxX(transparentSubviewRect), NSMaxY(transparentSubviewRect))];
[path closePath];
[path moveToPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
[path curveToPoint: NSMakePoint(NSMaxX(rect), NSMinY(rect))
controlPoint1: NSMakePoint(NSMaxX(transparentSubviewRect), NSMaxY(transparentSubviewRect))
controlPoint2: NSMakePoint(NSMaxX(rect), NSMinY(transparentSubviewRect))];
[path lineToPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))];
[path lineToPoint:NSMakePoint(NSMinX(rect), NSMaxY(rect))];
[path lineToPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
[path lineToPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
[path closePath];
[[NSColor windowBackgroundColor] setFill];
[path fill];
}

@end

#import "MyWindow.h"

@implementation MyWindow


- (BOOL)isOpaque
{
return NO;
}

- (NSColor *)backgroundColor
{
return [NSColor colorWithCalibratedWhite:0.0 alpha:0.3];
}

@end

最佳答案

更简单的解决方案是填充整个 View 并使用 NSCompositingOperationClear 操作。强制性的是不透明窗口+窗口的一些透明背景颜色。

- (void)drawRect:(NSRect)dirtyRect {
[[NSColor windowBackgroundColor] setFill];
NSRectFill([self bounds]);
NSRectFillUsingOperation(transparentSubviewRect, NSCompositingOperationClear);
}

关于macos - 如何使用不透明窗口创建内部透明 nsview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com