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

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

[[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就完成了。

参考资料

自定义 Video.js 的全屏按钮,您需要按照以下步骤进行操作: 1. 创建一个全屏按钮:您可以使用 HTML 和 CSS 创建一个自定义全屏按钮。 2. 使用 Video.js 的 API:使用 Video.js 的 API 将全屏按钮添加到视频播放器中。 3. 添加事件监听器:使用 Video.js 的 API 添加事件监听器,以便在用户点击自定义全屏按钮时触发事件。 下面是一个示例代码,用于自定义 Video.js 的全屏按钮: ```html <!DOCTYPE html> <html> <head> <link href="https://vjs.zencdn.net/7.8.4/video-js.css" rel="stylesheet" /> <script src="https://vjs.zencdn.net/7.8.4/video.js"></script> </head> <body> <video id="my-video" class="video-js" controls preload="auto" width="640" height="264" data-setup="{}"> <source src="my-video.mp4" type="video/mp4" /> </video> <style> .vjs-custom-fullscreen-control { background-color: #555; border: none; color: #fff; cursor: pointer; font-size: 16px; padding: 5px 10px; } .vjs-custom-fullscreen-control:hover { background-color: #333; } </style> <script> var player = videojs('my-video'); // 添加自定义全屏按钮 var fullscreenButton = player.controlBar.addChild('button', { className: 'vjs-custom-fullscreen-control', text: 'Fullscreen' }); // 将全屏按钮添加到全屏控件组中 player.controlBar.fullscreenToggle.addChild(fullscreenButton); // 添加事件监听器 fullscreenButton.on('click', function() { player.requestFullscreen(); }); </script> </body> </html> ``` 在这个示例代码中,我们创建了一个类名为 `vjs-custom-fullscreen-control` 的自定义全屏按钮,并将其添加到了 Video.js 的全屏控件组中。然后,我们添加了一个事件监听器,当用户点击自定义全屏按钮时,视频播放器将进入全屏模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值