透明的NSWindow +自定义NSView - 不希望自定义绘制的对象投射阴影?

时间:2016-02-10 17:19:23

标签: macos cocoa nswindow

所以我有一个无边框窗口,它的背景颜色设置为清除。在它里面,我有一个带有一些自定义绘图的NSView。它看起来像这样:

https://www.dropbox.com/s/16dh9ez3z04eic7/Screenshot%202016-02-10%2012.15.35.png?dl=0

问题是,自定义绘图正在投射阴影,如果视图被隐藏,您可以看到它:

https://www.dropbox.com/s/04ymp0b2yqi5egu/Screenshot%202016-02-10%2012.19.04.png?dl=0

我只想要窗户框架周围的阴影!我怎样才能做到这一点?

注意:奇怪的是,给窗口一个标题栏可以提供我想要的行为:https://www.dropbox.com/s/1vv9iwb5403tufe/Screenshot%202016-02-10%2012.25.29.png?dl=0 - 不幸的是,我不想要标题栏。

代码:

@implementation MyWindow

/*
 In Interface Builder, the class for the window is set to this subclass. Overriding the initializer
 provides a mechanism for controlling how objects of this class are created.
 */
- (id)initWithContentRect:(NSRect)contentRect
                styleMask:(NSUInteger)aStyle
                  backing:(NSBackingStoreType)bufferingType
                    defer:(BOOL)flag {

    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:NO];

    if (self != nil) {
        [self setOpaque:NO];
        [self setBackgroundColor:[NSColor clearColor]];

        [self makeKeyAndOrderFront:NSApp];
        [self setMovable:YES];
        [self setShowsResizeIndicator:YES];
        [self setResizeIncrements:NSMakeSize(2, 2)];
        [self setLevel:TOP_LEVEL];
    }
    return self;
}

/*
 Custom windows that use the NSBorderlessWindowMask can't become key by default. Override this method
 so that controls in this window will be enabled.
 */
- (BOOL)canBecomeKeyWindow {
    return YES;
}

@end

ContentView:

@implementation WindowContentView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // we need to redraw the whole frame
    dirtyRect = self.bounds;

    // background
    NSBezierPath *framePath = [NSBezierPath bezierPathWithRoundedRect:dirtyRect xRadius:0.0f yRadius:0.0f];
    [[NSColor colorWithCalibratedRed:0.0f/255.0f green:255.0f/255.0f blue:153.0f/255.0f alpha:0.3f] setFill];
    [framePath fill];

    // border
    [[NSColor colorWithCalibratedRed:0.0f/255.0f green:255.0f/255.0f blue:153.0f/255.0f alpha:1.0f] setStroke];
    framePath.lineWidth = 1.0f;
    [framePath stroke];

    // grid
    [self drawGridInRect:dirtyRect];

    [super drawRect:dirtyRect];
}

- (void)drawGridInRect:(NSRect)rect {
    int rows = 5;
    int columns = 5;

    float gridWidth = CGRectGetWidth(rect)/columns;
    float gridHeight = CGRectGetHeight(rect)/rows;

    [[NSColor colorWithCalibratedRed:0.0f/255.0f green:255.0f/255.0f blue:153.0f/255.0f alpha:0.2f] setStroke];
    [NSBezierPath setDefaultLineWidth:1.0f];

    for(int i=1; i<rows; i++) {
        [NSBezierPath strokeLineFromPoint:NSMakePoint(gridWidth*i, CGRectGetMaxY(rect))
                                  toPoint:NSMakePoint(gridWidth*i, CGRectGetMinY(rect))];
    }

    for(int i=1; i<columns; i++) {
        [NSBezierPath strokeLineFromPoint:NSMakePoint(CGRectGetMaxX(rect), gridHeight*i)
                                  toPoint:NSMakePoint(CGRectGetMinX(rect), gridHeight*i)];
    }
}

@end

0 个答案:

没有答案