3 Replies
      Latest reply on Dec 6, 2019 10:15 AM by Claude31
      Ammar S. Mitoori Level 1 Level 1 (0 points)

        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

        • Re: UIHoverGestureRecognizer Demo Project Error
          Claude31 Level 8 Level 8 (7,245 points)

          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.

            • Re: UIHoverGestureRecognizer Demo Project Error
              Ammar S. Mitoori Level 1 Level 1 (0 points)

              thanks allot let me try it and see

                • Re: UIHoverGestureRecognizer Demo Project Error
                  Claude31 Level 8 Level 8 (7,245 points)

                  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