本文介绍了mouseEntered和mouseExited在NSImageView子类中未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了

I created a subclass of NSImageView to capture mouseEntered and mouseExited events.But only mouseUp and mouseDown events are getting called. How to capture the mouseEntered and mouseExited events in NSImageView subclass?

推荐答案

如果您想使用 mouseEntered:和 mouseExited:您需要使用 NSTrackingArea 。以下是的参考。

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

示例:

//Add this to Your imageView subclass

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

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

-(void)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];
}

这篇关于mouseEntered和mouseExited在NSImageView子类中未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 04:51