Cocoa: change cursor when it's over an NSButton

一笑奈何 提交于 2019-11-26 16:43:45

问题


How can I change the cursor when it's over an NSButton?


回答1:


[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];



回答2:


You should subclass NSButton first, then add the code below.

- (void)resetCursorRects
{
    if (self.cursor) {
        [self addCursorRect:[self bounds] cursor: self.cursor];
    } else {
        [super resetCursorRects];
    }
}

Now you can set cursor as you like.

[self.button setCursor:[NSCursor pointingHandCursor]];

Note: add cursor as a property of your subclass, like this:

@property (strong) NSCursor *cursor;  



回答3:


Following the already given example, creating a subclass of NSButton in Swift 5:

import AppKit

class Button: NSButton {
    override func resetCursorRects() {
        addCursorRect(bounds, cursor: .pointingHand)
    }
}



回答4:


Have the button add a cursor rect.



来源:https://stackoverflow.com/questions/2925580/cocoa-change-cursor-when-its-over-an-nsbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!