【问题标题】:Find a index of a scroll View查找滚动视图的索引
【发布时间】:2020-02-23 01:07:37
【问题描述】:

我有一个显示数组项的水平滚动视图。

如何读取用户长按的滚动视图的索引。

使用 .onLongPressGesture {“要执行的操作”} 我如何读取要在我的函数中传递的索引以删除项目。

ScrollView(.horizontal, content: {
                        HStack(spacing: 100) {
                            ForEach(post, id: \.self){ item in


                                ZStack {
                                    Rectangle().foregroundColor(.blue).frame(width: 190, height: 170, alignment: .center)
                                    Text(item)

                                }.onTapGesture {
                                    self.temp = item


                                }

                                .onLongPressGesture {
                                    // find index
                                    //perform delate at index
                                    //pass the index to the function delate
                                    self.delate(index: 0)
                                }

                            }
                            }

                        .padding(.leading, 10)
                    })

如果我手动编写索引 0,延迟工作正常,但我想传递用户长按的索引。 感谢您的帮助。

【问题讨论】:

    标签: arrays swift xcode scrollview swiftui


    【解决方案1】:

    试试这个:(更好的是你不必使用索引,你可以使用项目本身)

    struct ContentView: View {
    
        @State var post = ["test2", "test3", "test4"]
    
    
    
        var body: some View {
            ScrollView(.horizontal, content: {
                HStack(spacing: 100) {
                    ForEach(post, id: \.self){ item in
    
                        VStack {
                            Rectangle().foregroundColor(.blue).frame(width: 190, height: 170, alignment: .center)
                            Text(item).onTapGesture {
                                //          post.remove(at: post.index(item))
                                self.post.remove(at: self.post.index(of: item)!)
                            }
                        }
                    }
                    .padding(.leading, 10)
                }
            })
        }
    }
    

    【讨论】:

      【解决方案2】:

      我认为正确的方法是在开始时获取索引,而不是在 ViewBuilder 内部:

         ForEach(Array(post.enumerated()), id: \.element){ (index, element) in
          ....
          }
      

      完整的视图是这样的:

      struct ScrollViewHStackView: View{
      
      @State private var post = ["1","2","3","4","5"]
      @State private  var temp : String = ""
      
      func delete(index: Int){
       self.post.remove(at: index)
      }
      
      var body: some View {
      
       ScrollView(.horizontal, content: {
      //HStack(spacing: 100) {
          HStack{
           ForEach(Array(post.enumerated()), id: \.element){ (index, element) in
      
      
              ZStack {
                  Rectangle().foregroundColor(.blue).frame(width: 190, height: 170, alignment: .center)
                  Text(element)
      
                  }.onTapGesture {
                  self.temp = element
                  }
      
              .onLongPressGesture {
      
      
                  // find index
                  //perform delate at index
                  //pass the index to the function delate
                  self.delete(index: index)
              }
      
          }}
         // }
      
          .padding(.leading, 10)
          })}}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        • 2019-12-24
        • 1970-01-01
        • 2020-08-08
        • 2022-11-30
        相关资源
        最近更新 更多