NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Detect & use mouse wheel movement/event to call a function?

Forums > SwiftUI

Is there a way to use SwiftUI to call a function when the user scrolls the mouse wheel ( up or down ), while the mouse cursor is hovering over some lines of text?

Ultimately, I'm making a language learning app for MacOS, where the user can visually read sentences of text, then when they hover the cursor over the text - and at same time - move their mouse wheel ( up or down ), they can hear an audio file of the translation.

I'm using the condition ( cursor hover + mouse wheel event ), because I'm reserving other mouse events - or combinations of - for calling other functions ( such as showing visual transations, etc )

In CSS, I can use

<div class="smartText" onwheel="playAudioTransation()" onclick="showVisualTransation()" > How are you today? </div>

, but I haven't found the SwiftUI equivalent yet.

I was able to make a play-sound-file function work, so that part I already have.

But I can't figure out how to make some lines of text, listen for the user's cursor hover and mouse wheel movement, and then calls a function when these events happen.

Does anyone know how to do this in SwfitUI ? ( If buttons can be made to receive mouse hover + wheel events, that could work too )




Maybe try the solution from this StackOverflow answer?


I came across that StackOverflow page earlier in my Googling too, but sadly much of the solution giver's explanation and code are beyond my current understanding of SwiftUI.

I can see that on the StackOverflow page, the solution giver is creating a function called trackScrollWheel()

that runs onAppear of a view called "horizontal scroll" : .onAppear { trackScrollWheel() } and calls the function he's creating.

I'm guessing .filter { event in event?.type == .scrollWheel } specifies to only listen for scrollwheel.

Beyond that though, I'm not sure what he's doing, where this code would fit into my code of text lines ( which is just a simple vertical stack of text lines within var body: some View ), or where I would place my call for my own play-sound-file function.

I'm also not sure what he refers to by "you already have a hover/focus boolean for the view".


Your code would go in place of this line:

vm?.goBackOrForwardBy(delta: Int(event?.deltaY ?? 0))


Thanks, will give it a try.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.