可可:像窗户一样码头

时间:2010-08-01 15:04:46

标签: objective-c cocoa macos nswindow dock

我希望创建一个类似于停靠窗口的NSWindow: - 当鼠标光标停留在屏幕的一个边缘时出现 - 不关注焦点(具有焦点的应用程序保留它)但是会发现鼠标事件

关于如何实现这一点的任何想法?

先谢谢你的帮助,

1 个答案:

答案 0 :(得分:2)

您可以使用窗口的alpha值执行某些操作。使用NSView的这个子类作为窗口的内容视图。

#import <Cocoa/Cocoa.h>

@interface IEFMouseOverView : NSView {
    BOOL canHide;
    BOOL canShow;
}
- (id)initWithFrame:(NSRect)r;
@end


@interface IEFMouseOverView (PrivateMethods)
- (void)showWindow:(NSTimer *)theTimer;
- (void)hideWindow:(NSTimer *)theTimer;
@end

@implementation IEFMouseOverView
- (void)awakeFromNib {
    [[self window] setAcceptsMouseMovedEvents:YES];
    [self addTrackingRect:[self bounds] owner:self userData:nil
             assumeInside:NO];
}
- (id)initWithFrame:(NSRect)r {
    self = [super initWithFrame:r];
    if(self) {
        NSLog(@"Gutentag");
        [[self window] setAcceptsMouseMovedEvents:YES];
        [self addTrackingRect:[self bounds] owner:self userData:nil
                 assumeInside:NO];
    }
    return self;
}

- (void)mouseEntered:(NSEvent *)ev {
    canShow = YES;
    canHide = NO;
    NSTimer *showTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 
                                                          target:self 
                                                        selector:@selector(showWindow:) 
                                                        userInfo:nil 
                                                         repeats:YES];
    [showTimer fire];
}

- (void)mouseExited:(NSEvent *)ev {
    canShow = NO;
    canHide = YES;
    NSTimer *hideTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 
                                                          target:self 
                                                        selector:@selector(hideWindow:) 
                                                        userInfo:nil
                                                         repeats:YES];
    [hideTimer fire];
}

- (void)showWindow:(NSTimer *)theTimer {
    NSWindow *myWindow = [self window];
    float originalAlpha = [myWindow alphaValue];
    if(originalAlpha >= 1 || canShow == NO) {
        [theTimer invalidate];
        return;
    }
    [myWindow setAlphaValue:originalAlpha + 0.1];
}

- (void)hideWindow:(NSTimer *)theTimer {
    NSWindow *myWindow = [self window];
    float originalAlpha = [myWindow alphaValue];
    if(originalAlpha <= 0 || canHide == NO) {
        [theTimer invalidate];
        return;
    }
    [myWindow setAlphaValue:originalAlpha - 0.1];
}
@end