4

I've NSButton in NSView. The NSView has NSClickGestureRecognizer. Now I am able to get action from NSView that have NSClickGestureRecognizer. But the problem is NSButton is not able click.

How can I solve this problem?

Code:

import Cocoa

class ViewController: NSViewController {

    @IBAction func actionGesture(_ sender: NSClickGestureRecognizer) {

        print("Gesture Clicked!")
    }

    @IBAction func actionButton(_ sender: NSButton) {
             print("Button Clicked!")
    }

}

Screenshot:

enter image description here

Project Link: https://drive.google.com/open?id=1HmbKiqdF9z7SH131IyyfJ21j8KOgabzl

1 Answers1

3

The easyest way to solve this is to arrange the View structure like showen below. So the View connected to the NSClickGestureRecognizer is behind the button.

enter image description here

Hope this helps

Marc T.
  • 5,090
  • 1
  • 23
  • 40
  • Thanks! But, `NSButton` must be the `subView` of `Custom View`. Since, the `NSButton` action is triggered when delay click. But, I need it like normal NSButton & Gesture action. Because the button constraints depends upon the custom view –  May 27 '18 at 18:57
  • If so, you can make NSView clickable without a gesture recognizer by implementing mouseDown / mouseUp the classical way. – Marc T. May 27 '18 at 19:13
  • I've updated project link working button over gesture view. But, It's working only one view on parentView. I tried to find out but can't. If able to check then please –  May 28 '18 at 06:36
  • You did it some different as described from me. You implemented hitTest to check if its a NSButton class or not to ignore the gesture. My idea was to subclass NSView to implement func mouseUp(with event: NSEvent) or unc mouseDown(with event: NSEvent) without using NSClickGestureRecognizer. – Marc T. May 29 '18 at 06:30
  • Yeah I did originally what you told. My issue has been solved with NSTracking as you said. Thanks for your help!. Now, I'm just wondering why it's not working! No problem If not able to solve. –  May 29 '18 at 06:36