UIHoverGestureRecognizer Demo Project Error

Hi


I tried to caputre the hover event over a label as described from Apple in first link, I outlet for the label and added the gesture to it

and before that linked the gesture to a function, but I keep geting the error "Use of unresolved identifier UIHoverGestureRecognizer"

not sure why ? the simple demo is in dropbox link.

--

Kindest Regards


h ttps://developer.apple.com/documentation/uikit/uihovergesturerecognizer


h ttps://www.dropbox.com/s/c1tjrjtzmd6wk05/Gestures%20Demo.zip?dl=0

Accepted Reply

No, that works for me like this.


Could you send me the complete last project that does not work ?


Here is the code of the viewController


class ViewController: NSViewController { 

    @IBOutlet weak var label: LabelWithHelp!

    fileprivate var labelTrackingArea    : NSTrackingArea?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.window?.isOpaque = false
        if labelTrackingArea == nil {
            labelTrackingArea = NSTrackingArea(rect: label.bounds, options: [NSTrackingArea.Options.mouseEnteredAndExited, NSTrackingArea.Options.cursorUpdate, NSTrackingArea.Options.activeInKeyWindow], owner: label, userInfo: nil)
            label.addTrackingArea(labelTrackingArea!)
        }
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

}

Replies

You tried to implement on MacOS Project.


That's for iOS or Catalyst only (that's a UIGesture, not an NSGesture).

Note also that

UIHoverGestureRecognizer
has no effect when your app runs in iOS


For MacOS project, just use TrackingArea.

thanks allot let me try it and see

To implement trackingArea in MacOS, I did it that way:


Subclass UILabel to handle the mouse events


class LabelWithHelp: NSTextField {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        // Drawing code here. If needed
    }
    // MARK: - Mouse Events
  
    // --------------------- mouseEntered --------------------------------------------------------------
    //  Description: When hovering over label
    //  Parameters
    //      theEvent: NSEvent
    //  Comments :
    //      Need to have defined trackingArea
    // -------------------------------------------------------------------------------------------------
  
    override func mouseEntered(with theEvent: NSEvent) {
      
        super.mouseEntered(with: theEvent)
        print("Entered")
    }
  
    // --------------------- mouseExited --------------------------------------------------------------
    //  Description: Exit from hover
    //  Parameters
    //      theEvent: NSEvent
    //  Comments :
    // -------------------------------------------------------------------------------------------------
  
    override func mouseExited(with theEvent: NSEvent) {  
        super.mouseExited(with: theEvent)
        print("Exited")
        arrowCursor!.set()      
    }
  
    // --------------------- cursorUpdate --------------------------------------------------------------
    //  Description: set cursor as pointing hand
    //  Parameters
    //      event: NSEvent
    //  Comments :
    // -------------------------------------------------------------------------------------------------
  
    override func cursorUpdate(with event: NSEvent) {
            pointingHandCursor!.set()
    }

}


Declare for each object (here label) for which you want hovering (declare the right class in IB)


@IBOutlet var weak myLabel: LabelWithHelp!     
 fileprivate var labelTrackingArea     : NSTrackingArea?


In windowDidLoad


        if labelTrackingArea == nil {     // Avoid creating multiple times
            labelTrackingArea = NSTrackingArea(rect: myLabel.bounds, options: [NSTrackingArea.Options.mouseEnteredAndExited, NSTrackingArea.Options.cursorUpdate, NSTrackingArea.Options.activeInKeyWindow], owner: myLabel, userInfo: nil)
            myLabel.addTrackingArea(labelTrackingArea!)
        }

The options allow for tracking mouse in, mouse out and change cursor if needed


NOTE: there is also a very simple way: use tooltips


@IBOutlet var weak myLabel: UILabel!

myLabel.toolTip = "Hover over label"


You have not as much control, but so simple.

You can even declare it directly in IB, in Identity inspector

Working ? Hope so. If it does, don't forget to close the thread.

Hi


thanks allot for the follow up and very sorry for the delay, I'll test it today and get back to you.

--

kindest Regards

How did the test go ?

Hi, Sorry for the delay


I tried to implement the code, now I used the class defining code inside the application default view controller but I got 2 errors of un resolved identifier for arrowCursor and pointingHandCursor ?


h ttps://www.dropbox.com/s/dgse44cg138sp1e/Gestures%20Demo.zip?dl=0

I forgot to post the definition


let pointingHandCursor  : NSCursor? = NSCursor.pointingHand 
let arrowCursor         : NSCursor? = NSCursor.arrow


That should work now.

Now theres no erros but nothing shows when hovering over the label I updated the Dropbox project, only thing i made

different is that I couldnt get the windowDidLoad func in the ViewController so I implemented the code in the viewDidLoad in the view controller is that wrong ?

No, that works for me like this.


Could you send me the complete last project that does not work ?


Here is the code of the viewController


class ViewController: NSViewController { 

    @IBOutlet weak var label: LabelWithHelp!

    fileprivate var labelTrackingArea    : NSTrackingArea?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.window?.isOpaque = false
        if labelTrackingArea == nil {
            labelTrackingArea = NSTrackingArea(rect: label.bounds, options: [NSTrackingArea.Options.mouseEnteredAndExited, NSTrackingArea.Options.cursorUpdate, NSTrackingArea.Options.activeInKeyWindow], owner: label, userInfo: nil)
            label.addTrackingArea(labelTrackingArea!)
        }
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

}

I think I did same code, link for project below, thanks allot.


h ttps://www.dropbox.com/s/dgse44cg138sp1e/Gestures%20Demo.zip?dl=0

You have 2 main problems:


class LabelWithHelp has been defined INSIDE the ViewController.

It must be a top level class.

=> Change the closing curly brackets


In IB, you have not defined the type of Label as LabelWithHelp


Do those both, it will work.


Notes.

Many other issues, which may just be because it is a working file.

1. You have a lot of space which are not space, leading to warnings: Non-breaking space (U+00A0) used instead of regular space

2. You defined a gesture non useful

3. There are 2 labels defined, second is not used.

Hi

The (U+00A0) problem happens when I copy code lines from the forum and paste it into Xcode. Thanks allot everything works fine now.

--

Kindest Regards