Mac自定义 关闭 最小化 全屏 按钮

来源:互联网 发布:c语言玫瑰花效果图 编辑:程序博客网 时间:2024/06/16 20:57

在自定义之前一定要将系统自动创建的按钮关闭     原文

[[self.window standardWindowButton:NSWindowCloseButton] setEnabled:NO];[[self.window standardWindowButton:NSWindowMiniaturizeButton] setEnabled:NO];[[self.window standardWindowButton:NSWindowFullScreenButton] setEnabled:NO];[[self.window standardWindowButton:NSWindowZoomButton] setEnabled:NO];

自定义左上角的三个控制按钮

这里我是将三个按钮写到了一个自定义View中,方便布局用。

首先是头文件中声明:

#import <Cocoa/Cocoa.h>@interface CustomWindowButtonView : NSView {@private    BOOL mouseInside_;    NSButton *closeButton_;    NSButton *minitButton_;    NSButton *zoomButton_;}@property (nonatomic, assign) BOOL mouseInside;@property (nonatomic, retain) NSButton *closeButton;@property (nonatomic, retain) NSButton *minitButton;@property (nonatomic, retain) NSButton *zoomButton;@end

mouseInside参数是用于判断鼠标是否在视图之中。

创建一个window button所用到的方法是+ standardWindowButton:forStyleMask:
文档中相关说明:

Returns a new instance of a given standard window button, sized appropriately for a given window style.

这里说明了,此方法是返回一个标准的window button实例,并且会设置合适的window style。
NSWindowButton取值如下:

enum {   NSWindowCloseButton,   NSWindowMiniaturizeButton,   NSWindowZoomButton,   NSWindowToolbarButton,   NSWindowDocumentIconButton,   NSWindowDocumentVersionsButton = 6,   NSWindowFullScreenButton,};typedef NSUInteger NSWindowButton;

名字很清楚的显示了window button的作用,这里我们需要自定义的是左边的三个按钮,所以需要用到的是NSWindowCloseButton(关闭窗口按钮),NSWindowMiniaturizeButton(最小化窗口按钮)和NSWindowZoomButton(最大化窗口按钮,不是全屏按钮)。

注意
在OS X 10.10之后的版本中,NSWindowZoomButton会变成全屏按钮,而不是10.10之前的最大化窗口按钮。

第二个参数设置成self.window.styleMask就OK了。

这里windows button需要实现系统的悬停,响应窗口和非响应窗口的不同效果,那么就要实现鼠标移入和移出的方法- mouseEntered:- mouseExited:,但是实现这个方法,需要重写- updateTrackingAreas方法,因为当大小或坐标改变后就会造成所指定的检测区域错误,所以我们需要重写updateTrackingAreas方法,将创建NSTrackingArea的工作放在其中。

- (void)updateTrackingAreas {    [super updateTrackingAreas];    NSTrackingArea *const trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect) owner:self userInfo:nil];    [self addTrackingArea:trackingArea];}

然后继续实现鼠标移入移出的方法:

- (void)mouseEntered:(NSEvent *)event {    [super mouseEntered:event];    self.mouseInside = YES;    [self setNeedsDisplayForStandardWindowButtons];}- (void)mouseExited:(NSEvent *)event {    [super mouseExited:event];    self.mouseInside = NO;    [self setNeedsDisplayForStandardWindowButtons];}

在这里我们需要实现一个在苹果的官方文档中并不存在的方法- _mouseInGroup:,这个方法用来返回判断鼠标是否在视图中的参数。

- (BOOL)_mouseInGroup:(NSButton *)button {    return self.mouseInside;}

最后一个就是需要重绘一下按钮的状态:

- (void)setNeedsDisplayForStandardWindowButtons {    [self.closeButton setNeedsDisplay];    [self.minitButton setNeedsDisplay];    [self.zoomButton setNeedsDisplay];}

这样我们就实现了自定义的window button,但是窗口在成为第一响应者和不适第一响应者的时候,还有当点击最小化的时候等等情况下,window button都会处于错误的状态中,例如,当失去第一响应的时候,这三个按钮应该变为淡灰色,但却是彩色,当点击最小化,然后再点击显示窗口的时候,三个按钮还是停留在鼠标移入的状态中,所以这些情况都要进行处理。这里我用通知实现了相关的处理:

首先,注册通知:

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];[defaultCenter addObserver:self selector:@selector(applicationWillBecomeActive:)                      name:NSApplicationWillBecomeActiveNotification object:NSApp];[defaultCenter addObserver:self selector:@selector(applicationDidResignActive:)                      name:NSApplicationDidResignActiveNotification object:NSApp];[defaultCenter addObserver:self selector:@selector(windowActiveChanged:)                       name:NSWindowDidBecomeMainNotification object:nil];

然后实现处理方法(如果有更好的处理方式,大家可以自行写逻辑,这里仅供参考):

- (void)applicationWillBecomeActive:(NSNotification *)notification {    self.mouseInside = YES;    [self setNeedsDisplayForStandardWindowButtons];    self.mouseInside = NO;    [self setNeedsDisplayForStandardWindowButtons];}- (void)applicationDidResignActive:(NSNotification *)notification {    self.mouseInside = NO;    [self setNeedsDisplayForStandardWindowButtons];}- (void)windowActiveChanged:(NSNotification *)notification {    self.mouseInside = NO;    [self setNeedsDisplayForStandardWindowButtons];}

这样左边的三个自定义window button就已经完成。

兼容10.10之前的按钮需要实现自定义全屏按钮(NSWindowFullScreenButton)

创建全屏按钮的方式与之前相同,不多说直接看代码:

if (self.versionNum != NSNotFound) {        self.fullScreenButton = [NSWindow standardWindowButton:NSWindowFullScreenButton forStyleMask:self.window.styleMask];        [self.fullScreenButton setFrame:NSMakeRect(0, 0, self.fullScreenButton.frame.size.width, self.fullScreenButton.frame.size.height)];        [self.topView.fullScreenBackView addSubview:self.fullScreenButton];}

在这里我判断了一下系统的版本。

注意

设置按钮一定要在主视图控制器中进行,不能在自定义的子视图中进行,否则在10.10之前的系统中会出现coreUI的错误提示。而且这个错误出现的几率不是100%

在全屏之后,我们最好将所有的window button给隐藏掉,调用setHidden:即可。

这下,自定义的window button就完成了。

参考资料

  • OSX的MouseEntered和MouseExited事件检测
  • Cocoa/OSX - NSWindow standardWindowButton behaving strangely once copied and added again
0 0
原创粉丝点击