Skip to main content
r
5mo ago

onTapGesture SwiftUI macOS

Hello! :)

1.) Problem: onTapGesture for a Rive .view() doesn't work on MacOS. It works on iOS. Verified the problem by creating a new Xcode macOS project and the problem is still there.
Usage: Rive-made buttons

2.) macOS, iOS, SwiftUI

3.)
Code:
``` @StateObject var rectangularButton = RiveViewModel(fileName: "Rectangular", fit: .contain)```
``` rectangularButton.view() .onTapGesture { print("Button tapped") }
```

Video:

Thank you very much Rive! :)

8 replies
D
r
D
5mo ago

Hey! Can you share your .riv / .rev file with us? In the mean time, I'll be trying with some files of our own.

r
5mo ago

Hello David,

I tried any Rive file and onTapGesture still doesn't work. How was it with you? Thank you :)

D
5mo ago

I'm able to reproduce with some of our files, and I am currently working on a fix (for both iOS and macOS). I think I have a solution, it's just a matter of testing. More soon!

r
5mo ago

Ok thank you very much David :)

D
5mo ago

We just released v6.0.7, which introduces a new property on RiveViewModel - forwardsListenerEvents. This defaults to false to preserve the old behavior, but if you set it to true, it should help solve your issue(s). Let me know if this fixes it for you! Here's an example of how to set it up:

@StateObject var viewModel: RiveViewModel = {
    let viewModel = RiveViewModel(fileName: "…")
    viewModel.forwardsListenerEvents = true
    return viewModel
}()

5mo ago
[deleted]
5mo ago
[deleted]
r
5mo ago

Wow onTapGesture works now thank you very much David! :) The only problem that I see now is that when I click on the button, it stays "button down"/"tapped", unlike before where it bounces back.
I posted a video so you can see the difference from the previous video. I'm not holding the clicking of the button.
But thank you very much David great work! :)

D
5mo ago

Hmm… I'm not able to reproduce this locally. Maybe somewhere in your view, SwiftUI is eating up the touches (if you're adding any other touch handling). Have you tried the simultaneousGesture modifier, perhaps? Alternatively, things to try: use a SwiftUI Button with an action and a Rive view as the label, or use Events to completely use Rive as a button without leveraging other SwiftUI views.

r
5mo ago

Hi David! Both simultaneousGesture and Button with an action and a Rive view didn't work but when I added rectangularButton.stop() inside onTapGesture, it started bouncing back again. Thank you very much for your help I appreciate it! :)