可可:像窗口一样停靠
Posted
技术标签:
【中文标题】可可:像窗口一样停靠【英文标题】:cocoa : dock like window 【发布时间】:2010-08-01 15:04:45 【问题描述】:我希望创建一个类似于停靠窗口的 NSWindow: - 当鼠标光标停留在屏幕的一个边缘时出现 - 不获取焦点(拥有焦点的应用会保持焦点)但会接收鼠标事件
知道如何实现这个吗?
提前感谢您的帮助,
【问题讨论】:
【参考方案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
【讨论】:
以上是关于可可:像窗口一样停靠的主要内容,如果未能解决你的问题,请参考以下文章