MacOS 监听鼠标进出视图

MacOS 监听鼠标进出视图,第1张

1. Objective-C实现

(1)创建OC新项目MonitorViewFromOC,在原有的视图中添加一个Custom View,添加新的类MonitorView,将其关联到Custom View,如图:

   (2)更改MonitorView中的updateTrackingAreas方法,通过mouseEntered和mouseExited监听鼠标进出视图


#import "MonitorView.h"

@implementation MonitorView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    // Drawing code here.
    self.layer.backgroundColor = [NSColor redColor].CGColor;
}

- (void)updateTrackingAreas {
    NSArray * trackingAreas = [self trackingAreas];
    for (NSTrackingArea *area in trackingAreas) {
        [self removeTrackingArea:area];
    }
    
    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                                options:(NSTrackingMouseEnteredAndExited |
                                                                         NSTrackingMouseMoved |
                                                                         NSTrackingActiveInActiveApp |
                                                                         NSTrackingInVisibleRect |
                                                                         NSTrackingAssumeInside |
                                                                         NSTrackingCursorUpdate)
                                                                  owner:self
                                                               userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void)mouseEntered:(NSEvent *)event {
    self.layer.backgroundColor = [NSColor greenColor].CGColor;
}

- (void)mouseExited:(NSEvent *)event {
    self.layer.backgroundColor = [NSColor redColor].CGColor;
}


@end

(3)检测到鼠标进入视图中时,视图变成绿色,离开视图时,视图变成红色

 

2.Swift实现 

步骤和OC一样,只需要将MonitorView.swift改成如下


import Cocoa

class MonitorView: NSView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        self.layer?.backgroundColor = NSColor.red.cgColor
        // Drawing code here.
    }
    
    override func updateTrackingAreas() {
        let trackingAreas = self.trackingAreas
        for area in trackingAreas {
            self.removeTrackingArea(area)
        }

        let trackingArea = NSTrackingArea.init(rect: self.bounds, options: [NSTrackingArea.Options.mouseEnteredAndExited,
                                                                            NSTrackingArea.Options.mouseMoved,
                                                                            NSTrackingArea.Options.activeInActiveApp,
                                                                            NSTrackingArea.Options.inVisibleRect,
                                                                            NSTrackingArea.Options.assumeInside,
                                                                            NSTrackingArea.Options.cursorUpdate],
                                               owner: self, userInfo: nil)
        
        self .addTrackingArea(trackingArea)
    
    }
    
    override func mouseEntered(with event: NSEvent) {
        self.layer?.backgroundColor = NSColor.green.cgColor
    }
    
    override func mouseExited(with event: NSEvent) {
        self.layer?.backgroundColor = NSColor.red.cgColor
    }
    
}

两个Demo:

CJY5588/MonitorView: MacOS 监听鼠标进出视图,Objective-C和Swift示例 (github.com)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/996675.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存