NSViewController缓慢注册鼠标事件?

Zol*_*tan 7 macos swift

我一直在为OS X上传一个上传menubar应用程序的小图片.我NSView为上传的项目创建了自定义子类.

这是默认情况下的样子:

馄饨 - 没有徘徊

鼠标事件由视图NSViewController按以下方式处理:

import Cocoa

class MenuItemController: NSViewController {

    private var trackingArea: NSTrackingArea?

    override func mouseEntered(theEvent: NSEvent) {
        if let v = self.view as? MenuItemView {
            v.shouldHighlight = true
            v.needsDisplay = true
        }
    }

    override func mouseExited(theEvent: NSEvent) {
        if let v = self.view as? MenuItemView {
            v.shouldHighlight = false
            v.needsDisplay = true
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        if (trackingArea == nil) {
            trackingArea = NSTrackingArea(rect: self.view.bounds, options: [.ActiveAlways, .MouseEnteredAndExited], owner: self, userInfo: nil)
            self.view.addTrackingArea(trackingArea!)
        }

        /* rest of the code... */
    }

}
Run Code Online (Sandbox Code Playgroud)

它工作正常,直到我快速移动光标的项目.看起来mouseExited()事件没有被调用,并且视图保持蓝色背景(鼠标实际上在Quit按钮上):

馄饨虫

我也尝试将鼠标处理移动到NSView,但结果相同.我感谢任何输入!谢谢!

Kei*_*ber 2

在我看来,苹果在这方面存在缺陷。

假设您根据苹果文档更新了跟踪区域,添加此附加修复程序可能会解决您的问题......它在很多情况下为我解决了这个问题。我在 mouseMoved / mouseEntered 例程中验证鼠标光标是否仍在我的视图框架内,如果不是,则调用 mouseExited: 我自己。

- (void) adjustTrackingArea
{   
   if ( trackingArea )
   {
      [self removeTrackingArea:trackingArea];
      [trackingArea release];
   }

   // determine the tracking options
   NSTrackingAreaOptions trackingOptions = // NSTrackingEnabledDuringMouseDrag | // don't track during drag
   NSTrackingMouseMoved |
   NSTrackingMouseEnteredAndExited |
   //NSTrackingActiveInActiveApp | NSTrackingActiveInKeyWindow | NSTrackingActiveWhenFirstResponder |
   NSTrackingActiveAlways;
   NSRect theRect = [self visibleRect];
   trackingArea = [[NSTrackingArea alloc]
                   initWithRect: theRect
                   options: trackingOptions
                   owner: self
                   userInfo: nil];
   [self addTrackingArea:trackingArea];
}


- (void)resetCursorRects
{
   [self adjustTrackingArea];
}
- (void)mouseEntered:(NSEvent *)ev
{
   [self setNeedsDisplay:YES];
   // make sure current mouse cursor location remains under the mouse cursor
   NSPoint cursorPt = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:NULL];

   // apple bug!!!
   //NSPoint cursorPt2 = [self convertPointFromBase:[ev locationInWindow]]; 
   //if ( cursorPt.x != cursorPt2.x )
   //   NSLog( @"hello old cursorPt" );
   NSRect r = [self frame];
   if ( cursorPt.x > NSMaxX( r ) || cursorPt.x < 0 )
   {
      [self mouseExited:ev];
      //cursorPt.x = [self convertPointFromBase:[ev locationInWindow]];
      //if ( cursorPt.x > NSMaxX( r ) || cursorPt.x < r.origin.x )
      return;
   }

   ... your custom stuff here ...
}

- (void)mouseExited:(NSEvent *)theEvent
{
   if ( isTrackingCursor == NO )
      return;

   [[NSCursor arrowCursor] set];
   isTrackingCursor = NO;
   [self setNeedsDisplay:YES];   
}

- (void)mouseMoved:(NSEvent *)theEvent
{
   [self mouseEntered:theEvent];
}
Run Code Online (Sandbox Code Playgroud)