macos 在 NSImageView 子类中未调用 mouseEntered 和 mouseExited

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11188034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:16:44  来源:igfitidea点击:

mouseEntered and mouseExited not called in NSImageView SubClass

objective-cmacoscocoansimageview

提问by Ram

I created a subclass of NSImageViewto capture mouseEnteredand mouseExitedevents. But only mouseUpand mouseDownevents are getting called. How to capture the mouseEnteredand mouseExitedevents in NSImageViewsubclass?

我创建了一个子类NSImageView来捕获mouseEnteredmouseExited事件。但只有mouseUpmouseDown事件被调用。如何捕获子类中的mouseEnteredmouseExited事件NSImageView

回答by Justin Boo

If You want to use mouseEntered:and mouseExited:You need to use NSTrackingArea. Here is reference NSTrackingArea Class Reference.

如果您想使用mouseEntered:并且mouseExited:您需要使用NSTrackingArea. 这是参考NSTrackingArea 类参考

Example:

例子:

//Add this to Your imageView subclass

-(void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"Mouse entered");
}

-(void)mouseExited:(NSEvent *)theEvent
{
    NSLog(@"Mouse exited");
}

-(void)updateTrackingAreas
{ 
    [super updateTrackingAreas];
    if(trackingArea != nil) {
        [self removeTrackingArea:trackingArea];
        [trackingArea release];
    }

    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
    trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
                                                 options:opts
                                                   owner:self
                                                userInfo:nil];
    [self addTrackingArea:trackingArea];
}

回答by Nikolay Suvandzhiev

Swift 3Version of @Justin Boo's answer:

@Justin Boo答案的Swift 3版本:

private var trackingArea: NSTrackingArea?

override func updateTrackingAreas() {
    super.updateTrackingAreas()

    if let trackingArea = self.trackingArea {
        self.removeTrackingArea(trackingArea)
    }

    let options: NSTrackingAreaOptions = [.mouseEnteredAndExited, .activeAlways]
    let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
    self.addTrackingArea(trackingArea)
}

回答by Xenon

Swift 4 version of Justin Boo's answer

Justin Boo 答案的Swift 4 版本

override func updateTrackingAreas() {
    for trackingArea in self.trackingAreas {
        self.removeTrackingArea(trackingArea)
    }

    let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeAlways]
    let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
    self.addTrackingArea(trackingArea)
}

回答by Dominique

C# Xamarin version of Justin Boo's answer

C# Xamarin 版本的 Justin Boo 的答案

public override void UpdateTrackingAreas ()
{
    base.UpdateTrackingAreas ();

    foreach (var item in TrackingAreas ()) {
        RemoveTrackingArea (item);
    }

    var options = NSTrackingAreaOptions.MouseEnteredAndExited | NSTrackingAreaOptions.ActiveAlways;

    var trackingArea = new NSTrackingArea (this.Bounds, options, this, null);

    AddTrackingArea (trackingArea);
}