1

I have a custom NSButton class and want to check (within viewWillDraw) if the users current mouse location is inside the buttons frame.

I get the mouse position using NSEvent.mouseLocation(), but that gives me an absolute NSPoint in relation to the screen itself.

self.frame (= NSButton.frame) instead returns some relative coordinates, refering to the buttons superview.

As a result, self.frame.contains(NSEvent.mouseLocation()) does not work and I have no idea how to solve that!

Appreciate any help!

ixany
  • 5,433
  • 9
  • 41
  • 65
  • You can try to convert the mouse position to the button frame.. In ios uiview has method to convert points from one view to others and vis versa. – Dasem Jan 21 '17 at 16:42
  • @Dasem Thanks for your reply! Couldn’t figure out how. `NSButton` isn’t a view. – ixany Jan 21 '17 at 17:42

1 Answers1

2

You can add NSTrackingArea with NSTrackingAreaOptions.MouseEnteredAndExited to your Button

Or you can use this code

Convert NSPoint to superview's coordinate system

let point = self.superview!.convert(self.window!.mouseLocationOutsideOfEventStream, from: nil)

And then just use self.frame.contains

if self.frame.contains(point)

Vitaly Migunov
  • 4,297
  • 2
  • 19
  • 23