在 SwiftUI 中的 ForEach 内向图像添加拖动手势

Tob*_*nce 4 gesture ios swift swiftui draggesture

我目前正在开发一个应用程序,向用户显示他的植物箱和一些测量值(如地面湿度、温度等)。每个植物箱里面都有一些植物,这些植物以详细视图显示。我希望用户能够将其中一株植物拖到角落以将其移除。目前我有这个实现:

@GestureState private var dragOffset = CGSize.zero

var body: some View {

    //Plants Headline
    VStack (spacing: 5) {
        Text("Plants")
            .font(.headline)
            .fontWeight(.light)

        //HStack for Plants Images
        HStack (spacing: 10) {

            //For each that loops through all of the plants inside the plant box
            ForEach(plantBoxViewModel.plants) { plant in

                //Image of the individual plant (obtained via http request to the backend)
                Image(base64String: plant.picture)
                    .resizable()
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.white, lineWidth: 2))
                    .offset(x: self.dragOffset.width, y: self.dragOffset.height)
                    .animation(.easeInOut)
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 70, height: 70)
                    .gesture(
                           DragGesture()
                            .updating(self.$dragOffset, body: { (value, state, transaction) in
                               state = value.translation
                           })
                   )
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

它看起来像下面这样: 代码片段的屏幕截图

但是,当我开始拖动时,两个图像都会移动。我认为这是因为两个图像的偏移量绑定到同一个变量(DragOffset)。我怎样才能做到只有一张图像在移动?

我考虑过实现某种数组,将植物的索引映射到特定的偏移量,但我不知道如何将其实现到手势中。谢谢你的帮助!

Asp*_*eri 5

这是可能的方法 - 在拖动过程中保持选择。使用 Xcode 11.4 / iOS 13.4 进行测试。

演示

@GestureState private var dragOffset = CGSize.zero
@State private var selected: Plant? = nil // << your plant type here
var body: some View {

    //Plants Headline
    VStack (spacing: 5) {
        Text("Plants")
            .font(.headline)
            .fontWeight(.light)

        //HStack for Plants Images
        HStack (spacing: 10) {

            //For each that loops through all of the plants inside the plant box
            ForEach(plantBoxViewModel.plants) { plant in

                //Image of the individual plant (obtained via http request to the backend)
                Image(base64String: plant.picture)
                    .resizable()
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.white, lineWidth: 2))
                    .offset(x: self.selected == plant ? self.dragOffset.width : 0,
                            y: self.selected == plant ? self.dragOffset.height : 0)
                    .animation(.easeInOut)
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 70, height: 70)
                    .gesture(
                        DragGesture()
                            .updating(self.$dragOffset, body: { (value, state, transaction) in
                                if nil == self.selected {
                                    self.selected = plant
                                }
                                state = value.translation
                            }).onEnded { _ in self.selected = nil }
                )
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)