mouseEntered和mouseExited在NSImageView子类中未调用 [英] mouseEntered and mouseExited not called in NSImageView SubClass

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

问题描述

我创建了 NSImageView 的子类来捕获 mouseEntered mouseExited 事件。
但是只有 mouseUp mouseDown 事件被调用。如何捕获 NSImageView 子类中的 mouseEntered mouseExited

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 。以下是 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子类中未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆