[英]Cocoa nsview change cursor
I tried to change the default cursor in my cocoa application.我试图更改可可应用程序中的默认光标。 I read about this but the standard approach isn't working for me.我读过这个,但标准方法对我不起作用。
I try to add to my OpenGLView subclass this method:我尝试将此方法添加到我的 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!");
}
It`s not working.它不工作。 Why?为什么?
There're two ways you can do it.有两种方法可以做到。 First - the most simple - is to change the cursor while the mouse enters the view and leaves it.首先——最简单的——是在鼠标进入和离开视图时改变光标。
- (void)mouseEntered:(NSEvent *)event
{
[super mouseEntered:event];
[[NSCursor pointingHandCursor] set];
}
- (void)mouseExited:(NSEvent *)event
{
[super mouseExited:event];
[[NSCursor arrowCursor] set];
}
Another way is to create tracking area (ie in awakeFromNib
-method), and override - (void)cursorUpdate:
-method另一种方法是创建跟踪区域(即在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];
}
For those of you looking for a Swift solution, the syntax is:对于那些正在寻找 Swift 解决方案的人,语法是:
override func mouseEntered(with event: NSEvent) {
super.mouseEntered(with: event)
NSCursor.pointingHand.set()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.