resetCursorRects not working (new cursor only flashes as mouse enters)

与世无争的帅哥 提交于 2019-12-10 10:38:42

问题


I'm creating a custom view that needs a special cursor (instead of the usual arrow). I'm using the resetCursorRects to setup the new cursor and its area, but the new cursor only appears briefly when the mouse enters the rect area, returning to arrow.

To check things better, I've created a new project, create a new custom view (based on NSView), add it to the window, but the problem remains. The custom view code:

#import "TestView.h"

@implementation TestView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    [[NSColor redColor]set];
    [NSBezierPath fillRect:self.bounds];
}

-(void)resetCursorRects
{
    [self addCursorRect:NSMakeRect(2, 2, 40, 40) cursor:[NSCursor openHandCursor]];
}

@end

I've tried things like [super resetCursorRects] or [self discardCursorRects] before the addCursorRect, but nothings happens. Am I doing anything wrong?


回答1:


Your code is correct and seems to work pretty fine in all cases.

There are some additional methods to set cursor image. Maybe they help you, 2 examples below.

1) You can set tracking area in your custom View and use NSResponder cursorUpdate: method in that view. Something like that:

// call when initializing view
- (void)updateTrackingAreas {
    [[NSTrackingArea alloc] initWithRect:self.visibleRect options: (NSTrackingCursorUpdate | NSTrackingEnabledDuringMouseDrag | NSTrackingActiveInActiveApp) owner:self userInfo:nil];
}

// NSResponder override in view
- (void)cursorUpdate:(NSEvent *)event {
    [[NSCursor openHandCursor] set];
}

2) If you are using your custom View in ScrollView you can use setDocumentCursor: method of ScrollView:

[self.scrollView setDocumentCursor:[NSCursor openHandCursor]];



回答2:


Solved with a reboot... After many hours of work, I discovered it was some sort of bug from the system, which was solved by a simple reboot.

Thank you all for your time...



来源:https://stackoverflow.com/questions/31816339/resetcursorrects-not-working-new-cursor-only-flashes-as-mouse-enters

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