Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在SwiftUI中将拖动手势添加到ForEach中的图像_Ios_Swift_Swiftui_Gesture_Draggesture - Fatal编程技术网

Ios 在SwiftUI中将拖动手势添加到ForEach中的图像

Ios 在SwiftUI中将拖动手势添加到ForEach中的图像,ios,swift,swiftui,gesture,draggesture,Ios,Swift,Swiftui,Gesture,Draggesture,我目前正在开发一个应用程序,向用户显示他的植物箱和一些测量值(如地面湿度、温度等) 每个植物框中都有一些植物,这些植物将显示在详细视图中。我希望用户能够将其中一株植物拖到一个角落,以便将其移除。 目前我有以下实现: @GestureState private var dragOffset = CGSize.zero var body: some View { //Plants Headline VStack (spacing: 5) { Text("Plant

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

@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
                           })
                   )
            }
        }
    }

}
如下所示:

但是,当我开始拖动时,两个图像都会移动。我认为这是因为两个图像的偏移量绑定到同一个变量(DragOffset)。如何实现只有一个图像在移动

我想实现某种数组,将植物的索引映射到一个特定的偏移量,但我不知道如何在手势中实现它。
谢谢你的帮助

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

@GestureState private var dragOffset=CGSize.zero

@选择的国有-私有var:工厂?=零//托比伦斯,欢迎光临。如果答案对你有效,那么在这里,这是一个好的公民实践,所以标记答案为“已接受”,并对其进行投票,以使社区更容易看到它。
@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 }
                )
            }
        }
    }

}