NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to detect the user hovering over a view

Paul Hudson    @twostraws   

Updated for Xcode 12.0

In SwiftUI, any macOS app or any iPadOS app where a mouse is connected can detect when the user is hovering their pointer over a view, and respond to it appropriately.

There are two modifiers you’ll want to use: onHover(), and hoverEffect(). The first of these allows you to track whether the pointer is currently hovering over the view, and is passed a Boolean reflecting that state. For example, this will color some text red or green depending on whether the pointer is hovering over it or not:

struct ContentView: View {
    @State private var overText = false

    var body: some View {
        Text("Hello, World!")
            .foregroundColor(overText ? Color.green : Color.red)
            .onHover { over in
                self.overText = over
            }
    }
}

The hoverEffect() modifier allows you to choose one of three ways the system can highlight the view when a hover happens: .highlight transforms the pointer into the shape of the view while creating a gentle directional effect, .lift transforms the pointer into the shape of the view while scaling up the view and placing a soft shadow behind it, and .automatic selects whichever highlight effect it thinks is most appropriate.

Note that .automatic is the default if you just apply the hoverEffect() modifier without any parameters, but it isn’t just selecting between .highlight and .lift – it’s quite a different effect, and won’t transform the pointer to match the shape of your view.

To try it out, here’s some code that places a tappable text label on the screen, giving it a .lift hover effect to make it clear that it’s tappable:

struct ContentView: View {
    var body: some View {
        Text("Tap me!")
            .font(.largeTitle)
            .hoverEffect(.lift)
            .onTapGesture {
                print("Text tapped")
            }
    }
}

Tip: To try this out on the iPadOS simulator, go to the I/O menu and choose Input > Send Cursor to Device.

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.8/5

Link copied to your pasteboard.