macOS开发 设置鼠标放在控件上是否有小手

 

1.ViewController.m引入头文件

#import "ViewController.h"
#import "VC1.h"

2.ViewController.m控件调用,用NSView举例

- (void)viewDidLoad {
    [super viewDidLoad];
      VC1 *view1 = [[VC1 alloc] init];

      view1.frame = CGRectMake(100, 100, 100, 100);

      [view1 setWantsLayer:YES];

       view1.layer.backgroundColor = CGColorCreateGenericRGB(1.0, 0, 0, 1.0);

      [self.view addSubview:view1];


}

3..在VC1.m中添加

@implementation VC1

//鼠标进入追踪区域
-(void)mouseEntered:(NSEvent *)event {
    NSLog(@"mouseEntered =========");
}

//mouserEntered之后调用
-(void)cursorUpdate:(NSEvent *)event {
    NSLog(@"cursorUpdate ==========");
    
    //更改鼠标光标样式
    [[NSCursor pointingHandCursor] set];
}

//鼠标退出追踪区域
-(void)mouseExited:(NSEvent *)event {
    NSLog(@"mouseExited ========");
    self.layer.backgroundColor = [NSColor yellowColor].CGColor;
}

//鼠标左键按下
-(void)mouseDown:(NSEvent *)event {
    //event.clickCount 不是累计数。双击时调用mouseDown两次,clickCount第一次=1,第二次 = 2.
    if ([event clickCount] > 1) {
        //双击相关处理
    }
    
    NSLog(@"mouseDown ==== clickCount: %ld  buttonNumber: %ld",event.clickCount,event.buttonNumber);
    
    self.layer.backgroundColor = [NSColor redColor].CGColor;
    
    //获取鼠标点击位置坐标:先获取event发生的window中的坐标,在转换成view视图坐标系坐标。
    NSPoint eventLocation = [event locationInWindow];
    NSPoint center = [self convertPoint:eventLocation fromView:nil];
    
    NSLog(@"center: %@",NSStringFromPoint(center));
    
    //判断是否按下了Command键
    if ([event modifierFlags] & NSEventModifierFlagCommand) {
        [self setFrameRotation:[self frameRotation] + 90.0];
        [self setNeedsDisplay:YES];
        
        NSLog(@"按下了Command键 ---- ");
    }
    
}

//鼠标左键起来
-(void)mouseUp:(NSEvent *)event {
    NSLog(@"mouseUp ======");
    
    self.layer.backgroundColor = [NSColor greenColor].CGColor;
}

//鼠标右键按下
- (void)rightMouseDown:(NSEvent *)event {
    NSLog(@"rightMouseDown =======");
    self.layer.backgroundColor = [NSColor blueColor].CGColor;
    
}

//鼠标右键起来
- (void)rightMouseUp:(NSEvent *)event {
    NSLog(@"rightMouseUp ======= ");
}

//鼠标移动
- (void)mouseMoved:(NSEvent *)event {
    NSLog(@"mouseMoved ========= ");
    self.layer.backgroundColor = [NSColor purpleColor].CGColor;
}

//鼠标按住左键进行拖拽
- (void)mouseDragged:(NSEvent *)event {
    NSLog(@"mouseDragged ======== ");
}

//鼠标按住右键进行拖拽
- (void)rightMouseDragged:(NSEvent *)event {
    NSLog(@"rightMouseDragged ======= ");
}

#pragma mark 监控区域要使用dirtyRect,而非self.frame,否则位置会错误。
- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
   
    [self addTrackingArea:[[NSTrackingArea alloc] initWithRect:dirtyRect options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved |
                           NSTrackingCursorUpdate |
                           NSTrackingActiveWhenFirstResponder |
                           NSTrackingActiveInKeyWindow |
                           NSTrackingActiveInActiveApp |
                           NSTrackingActiveAlways |
                           NSTrackingAssumeInside |
                           NSTrackingInVisibleRect |
                           NSTrackingEnabledDuringMouseDrag
                        owner:self userInfo:nil]];
    
    [self becomeFirstResponder];
    
}

运行走你

 

 

 

posted on 2022-03-10 14:37  高彰  阅读(164)  评论(0编辑  收藏  举报

导航