Class

UIHoverGestureRecognizer

A gesture recognizer that looks for pointer movement over a view.

Declaration

@interface UIHoverGestureRecognizer : UIGestureRecognizer

Overview

On macOS devices, a user can move the pointer over user interface elements. For some UI designs, it’s important to know when the pointer moves over an element, with no other user interactions, such as clicking the mouse button. The text for a hyperlink, for instance, may change colors or become underlined as the pointer moves over the link. This creates a rollover effect.

To provide this experience in your app, add a hover gesture recognizer that reacts as the pointer moves over the view. Provide the gesture recognizer with a target and action that the system calls when the pointer enters, exits, and moves across the view. The following code shows an example.

Listing 1

The button's default color changes to red as the pointer moves over the button

class ViewController: UIViewController {

    @IBOutlet var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let hover = UIHoverGestureRecognizer(target: self, action: #selector(hovering(_:)))
        button.addGestureRecognizer(hover)
    }

    @objc
    func hovering(_ recognizer: UIHoverGestureRecognizer) {
        switch recognizer.state {
        case .began, .changed:
            button.titleLabel?.textColor = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
        case .ended:
            button.titleLabel?.textColor = UIColor.link
        default:
            break
        }
    }
}

UIHoverGestureRecognizer has no effect when your app runs in iOS.

Relationships

Inherits From

See Also

Standard Gestures

Handling UIKit Gestures

Use gesture recognizers to simplify touch handling and create a consistent user experience.

Coordinating Multiple Gesture Recognizers

Discover how to use multiple gesture recognizers on the same view.

UILongPressGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for long-press gestures.

UIPanGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for panning (dragging) gestures.

UIPinchGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for pinching gestures involving two touches.

UIRotationGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for rotation gestures involving two touches.

UIScreenEdgePanGestureRecognizer

A gesture recognizer that looks for panning (dragging) gestures that start near an edge of the screen.

UISwipeGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for swiping gestures in one or more directions.

UITapGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for single or multiple taps.