I have a SwiftUI app that behaves similarly to Tinder (built using this guide - or see source code). You just swipe a stack of cards left and right. I have a "thumbs up" and "thumbs down" button as alternatives to swiping, but when they are pressed, I'd like it to swipe the card away for the user as if they had swiped it themselves.
Are there any ways to do this in SwiftUI without UIKit?
If your thumbs up and down buttons were part of the
CardViewthen it would be quite simple to achieve: just repeat whatever code executes on gesture inside a button action. Something along these lines:But, if your buttons reside outside of the
CardView, I think you'll have to re-architect your views a little bit. I would start by convertingswipeStatusfrom@Stateinto a@Binding, so its value can be passed down from the parent view. Then you will need to declare a state variable in yourContentView, which you can then tie together withswipeStatusbinding. Something like:Within your card view you'll need to react to the bound value changing. I just appended
onChangeto the mainVStack:I feel like there's a more elegant way of achieving this. I'm not a big fan of the
onRemovecallback,asyncAfterhack, and the fact that they render all cards at once. But at least that's a start.