Re: Mouse over
Re: Mouse over
- Subject: Re: Mouse over
- From: Jeremy Dronfield <email@hidden>
- Date: Sun, 5 Dec 2004 09:38:34 +0000
On 5 Dec 2004, at 4:52 am, Danny Swarzman wrote:
I'm sure this is really simple but I didn't see it. I want to show a
special image when the mouse goes over a thing. The thing can be a
button or can be implemented with another view class. It just needs to
show an image and sense the mouse.
I tried overriding mouseEntered in an NSButton subclass but the
functions doesn't get called.
I know I'm missing something obvious. At least I hope it's obvious to
someone.
Your control needs to have a tracking rect added to track mouse
movement within its frame. Note that the tracking rect will need to be
reset if your control's frame changes. Here's an example:
@interface MyCustomView : NSView // (or NSControl or NSButton)
{
NSTrackingRectTag myTrackingRect;
}
- (void)resetTrackingRect;
@end
@implementation MyCustomView
- (void)awakeFromNib
{
// Register your control to pick up frame changed notifications so you
can adjust the tracking rect.
// If you don't, and its frame changes (e.g. the window or the control
is resized),
// the tracking rect will get left behind
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(frameDidChange:)
name:NSViewFrameDidChangeNotification object:self];
[self setPostsFrameChangedNotifications:YES];
[self resetTrackingRect];
}
- (void)dealloc
{
// Remove your view as an observer and remove the tracking rect
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self removeTrackingRect:myTrackingRect];
[super dealloc];
}
- (void)frameDidChange:(NSNotification *)notification
{
// frame changed, so reset the tracking rect
if ([notification object] == self)
[self resetTrackingRect];
}
- (void)resetTrackingRect
{
if (myTrackingRect)
[self removeTrackingRect:myTrackingRect];
NSRect trackingRectFrame = [self frame];
trackingRectFrame.origin = NSZeroPoint;
myTrackingRect = [self addTrackingRect:trackingRectFrame owner:self
userData:nil assumeInside:NO];
}
- (void)mouseEntered:(NSEvent *)theEvent
{
// do something
[self setNeedsDisplay:YES];
}
- (void)mouseExited:(NSEvent *)theEvent
{
// do something
[self setNeedsDisplay:YES];
}
@end
Hope this helps.
Regards,
Jeremy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >Mouse over (From: Danny Swarzman <email@hidden>) |