sch*_*liy 6 macos cocoa objective-c nscursor
我试图在我的cocoa应用程序中更改默认光标.我读到了这个,但标准方法对我不起作用.
我尝试将此方法添加到我的OpenGLView子类中:
- (void) resetCursorRects
{
[super resetCursorRects];
NSCursor * myCur = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"1.png"] hotSpot:NSMakePoint(8,0)];
[self addCursorRect: [self bounds]
cursor: myCur];
NSLog(@"Reset cursor rect!");
}
Run Code Online (Sandbox Code Playgroud)
它不起作用.为什么?
Ast*_*ria 11
有两种方法可以做到.首先 - 最简单的 - 是在鼠标进入视图并离开时更改光标.
- (void)mouseEntered:(NSEvent *)event
{
[super mouseEntered:event];
[[NSCursor pointingHandCursor] set];
}
- (void)mouseExited:(NSEvent *)event
{
[super mouseExited:event];
[[NSCursor arrowCursor] set];
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建跟踪区域(即in awakeFromNib-method),并覆盖- (void)cursorUpdate:-method
- (void)createTrackingArea
{
NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingCursorUpdate;
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:self.bounds options:options owner:self userInfo:nil];
[self addTrackingArea:area];
}
- (void)cursorUpdate:(NSEvent *)event
{
[[NSCursor pointingHandCursor] set];
}
Run Code Online (Sandbox Code Playgroud)
对于那些正在寻找 Swift 解决方案的人来说,语法是:
override func mouseEntered(with event: NSEvent) {
super.mouseEntered(with: event)
NSCursor.pointingHand.set()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5339 次 |
| 最近记录: |