3

I'm new to SwiftUI. I'm showing a series of items in an HStack. They don't need to move, but I'd like to highlight them as the user drags their finger over them, and change a separate view somewhere else to give more information about the highlighted item, without the user having to lift their finger. They should be able to slide back and forth and see the item under their finger highlight, and the separate view's content update.

In UIKit, I'd create a UIPanGestureRecognizer, and as the gesture is recognized, get the correct view for the recognizer's location, set the view to be highlighted, set any other views to be un-highlighted, and update the detail view.

I can't figure out how to do this in SwiftUI, though. I don't want to drag anything around, I just want to get which view is underneath the user's finger as they slide it around.

CompC
  • 381
  • 2
  • 19

1 Answers1

1

I guess something like this can help you:

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
            Text("Hello, World!")
        }
        .gesture(
            DragGesture()
                .onChanged({ value in print(value) })
                .onEnded({ _ in print("End")})
        )
    }
}
wzso
  • 3,482
  • 5
  • 27
  • 48
  • How can I get which view in the stack the user is touching from inside the `onChanged` block? – CompC Aug 18 '20 at 14:01
  • @CompC I don’t quite understand your purpose. In SwiftUI’s realm, you should focus on the concrete view as in UIKit’s realm. You should focus on the state and its binding to SwiftUI’s ‘View’. – wzso Aug 18 '20 at 14:32
  • I'm using both SwiftUI and UIKit in the same app. I have a control built in SwiftUI and when the user drags over the views in the HStack, the UIViewController which contains the SwiftUI view needs to get a callback with the index of the view in the HStack that the user is touching. – CompC Aug 18 '20 at 14:38