4

I have a MacOS app which uses NSButton objects. The button is set to style "Push" and type "Push On Off".

I have created a class called DraggableButton which attempts to override the function:

override func mouseDragged(event: NSEvent){
     print("Mouse dragged!")
}

However it only overrides it if I also override this function AND refrain from calling super:

mouseDown(event: NSEvent){
     //Do Nothing. Everything works
}

If mouseDown is empty, then mouseDragged gets called. If however mouseDown calls super.mouseDown(event), or if I do not override mouseDown at all, then mouseDragged does not get called. Unfortunately, these actions disable the normal mouseDown functions.

mouseDown(event: NSEvent){
     super.mouseDown(event: event) //Now mouseDragged stops working
}

In short, how to I override mouseDragged without breaking the mouseDown function?

dmann200
  • 529
  • 4
  • 11
  • I can't reproduce your issue. It does work even if I call `super.moveDown(event)`. Note that your methods signatures are wrong. Should be `override func mouseDragged(with event: NSEvent) {` and `override func mouseDown(with event: NSEvent) {` [screenshot](https://www.dropbox.com/s/j4ly1vdoz8cw6ed/mouse%20dragged.jpg?dl=1) – Leo Dabus Oct 02 '20 at 21:53
  • Make sure to put your code to be executed before calling super. if you would like to propagate the mouseDown call `nextResponder?.mouseDown(with: event)` instead of `super.moveDown(event)` – Leo Dabus Oct 02 '20 at 22:11
  • Use `NSPanGestureRecognizer`? – Willeke Oct 03 '20 at 08:46

0 Answers0