objective-c - ratón - mouseadapter
mouseEntered y mouseExited no se llama en NSImageView SubClass (4)
Si desea usar mouseEntered:
y mouseExited:
necesita usar NSTrackingArea
. Aquí está la referencia NSTrackingArea Class Reference .
Ejemplo:
//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];
}
NSImageView
una subclase de NSImageView
para capturar eventos mouseEntered
y mouseExited
. Pero solo los eventos mouseUp
y mouseDown
son llamados. ¿Cómo capturar los eventos mouseEntered
y mouseExited
en la subclase NSImageView
?
Swift 4 versión de la respuesta de Justin Boo
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)
}
Versión C # Xamarin de la respuesta de 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);
}
Swift 3 versión de la respuesta de @Justin Boo :
private var trackingArea: NSTrackingArea?
override func 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)
}