How to show an overlay on hover?

Hi,

I'm trying to toggle the visibility of an overlay by using hovering. However, I found onHover() and onContinuousHover() don't work on visionOS. Is it intended?

Is there an solution for my case?

Post not yet marked as solved Up vote post of huanxiangwang Down vote post of huanxiangwang
1.9k views
  • Same here. I'm wanting to do a cursor for drawing the way the Vision version of the Freehand app does, but it looks like that isn't possible at this time?

Add a Comment

Replies

Hi @huanxiangwang , take a look at this video on "Enhance your iPad and iPhone apps for the Shared Space" from WWDC23, especially the Interaction part starting at 1 minute. This should help answer your question.

Hi @sha921 , thanks for your input. However, I'm building a native visionOS app from ground up. I'm pretty sure onHover() and onContinuousHover() don't work on native visionOS apps. I'm not sure if it's a bug or delibarately done so for privacy reasons as I heard from multiple WWDC23 sessions that where user is looking at is detected at the system level not the app level. There is also no way to extend the HoverEffect to create my own.

Basically what I would like to achive is to display an overlay above an image when the user is looking at it, and I found it's impossible. Below is an abstract code snippet.

struct ImageView: View {
    @State private var isHovering = false

    var body: some View {
        someImage
            .onHover(perform: { hover in
                isHovering = hover
            })
            .overlay(alignment: .bottom, content: {
                if isHovering {
                    imageInfo
                }
            })
    }
 }

Hey @sha921, I'm also trying to develop on visionOS, and I've noticed the same thing. I believe it might be tied to privacy constraints. It's odd that there isn't a clear way to implement onHover behaviors for native apps, especially when it seems like a fundamental feature. Have you come across any workarounds or alternatives to achieve similar interactivity? I'd appreciate any pointers! here you can get more help https://developer.apple.com/support/

Yeah, it would be cool to show controls based on hover state.

Hi all, the only way to use hover effects in visionOS with SwiftUI is to do something like this:

var body: some View {
     Rectangle()
          .contentShape(.hoverEffect, RoundedRectangle(cornerRadius: 10))
          .hoverEffect()
} 

This would put a RoundedRectangle hover effect on top of the Rectangle, of course this can be customized however you want. There are also three options (automatic, highlight, and lift) that you can apply like .hoverEffect(.lift), etc

Due to privacy, there are no custom hover effects like changing color, expanding controls, showing overlays, etc

Hope it's added in the future as it seems like a fundamental feature that users would expect. Otherwise we need to show too much information on the screen, or require taps that could be accomplished by just glancing at an entity.

.onHover should work for visionPro, I don't understand why is disabled, can some one from apple confirm that this function is not going to be included in the final release ?

  • I don't think you'll get an answer from Apple about this - they've clearly stated that one of the privacy protections they enforce is that anapplication can't know where a user is looking. Eye tracking is specifically prevented. Allowing an application to perform arbitrary code on hover would allow a developer to get precisely this information. In my use case I'd be happy if they'd allow us to specify an alternate visual representation to use on hover without knowing it was being displayed.

Add a Comment

It works in the Simulator only with this tool (choose and click on your window):