【问题标题】:cocoa : dock like window可可:像窗口一样停靠
【发布时间】:2010-08-01 15:04:45
【问题描述】:

我希望创建一个类似于停靠窗口的 NSWindow: - 当鼠标光标停留在屏幕的一个边缘时出现 - 不获取焦点(拥有焦点的应用会保持焦点)但会接收鼠标事件

知道如何实现这个吗?

提前感谢您的帮助,

【问题讨论】:

    标签: objective-c cocoa macos nswindow dock


    【解决方案1】:

    您可以对窗口的 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
    

    【讨论】:

      猜你喜欢
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多