2010-08-01 50 views
0

我正在創建一個NSWindow,其行爲與停靠窗口類似: - 當鼠標光標停留在屏幕的一個邊緣時出現 - 不需要焦點(具有焦點的應用程序保持它)但能夠欣賞鼠標事件cocoa:dock like window

關於如何實現這一點的任何想法?

在此先感謝您的幫助,

回答

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