简体   繁体   English

SwiftUI 拖动手势冻结多点触控

[英]SwiftUI drag gesture freezes with multi-touch

I am trying to drag a view in SwiftUI.我正在尝试在 SwiftUI 中拖动视图。

The drag works perfectly until I place my second finger on screen, after that the drag stops and neither of the code blocks ( onChanged , onEnded ) gets called.拖动效果很好,直到我将第二根手指放在屏幕上,之后拖动停止并且代码块( onChangedonEnded )都不会被调用。

But, when I again start dragging with a single finger, it again starts working.但是,当我再次开始用单指拖动时,它又开始工作了。

Is there any work around to fix this or I am missing something?有什么办法可以解决这个问题还是我遗漏了什么?

struct Pager: View {
  func drag(geometry: GeometryProxy) -> some Gesture {
    DragGesture()
    .onChanged({ (value) in
      //some code
    })
    .onEnded({ (value) in
      //some code
    })
  }

  var body: some View {
    GeometryReader { (geometry) in
      ZStack {
        ForEach(self.items.indices.reversed(), id: \.self) { index in
          Card(index: index, item: self.items[index])
            .offset(x: 0, y: self.offset(index: index, geometry: geometry))
            .animation(.linear)
        }
      }
      .background(Color.black)
      .gesture(self.drag(geometry: geometry))
    }
  }
}

You can use the same solution as this post , as I believe it is the same underlying problem.您可以使用与这篇文章相同的解决方案,因为我相信这是相同的潜在问题。 The solution employs a GestureState, which (as stated in the linked post) is a temporary value, and returns back to its initial state (0, 0) when the gesture is completed/interrupted.该解决方案使用 GestureState,它(如链接帖子中所述)是一个临时值,并在手势完成/中断时返回到其初始 state (0, 0)。

Your code should look something like this when using the GestureState (though you may be able to adapt it to use a separate drag method like in your original post):使用 GestureState 时,您的代码应如下所示(尽管您可以对其进行调整以使用单独的拖动方法,如原始帖子中所示):

struct Pager: View {
  @GestureState private var dragPosition = CGSize.zero

  var body: some View {
      ZStack {
        ForEach(self.items.indices.reversed(), id: \.self) { index in
          Card(index: index, item: self.items[index])
            .offset(x: 0, y: self.dragPosition.height) // This will now map the y offset of the card to the dragPosition GestureState. If you want the x offset use self.dragPosition.width
            .animation(.linear)
        }
      }
      .background(Color.black)
      .gesture(
          DragGesture()
             .updating($dragPosition) { (value, gestureState, transaction) in
                 gestureState = CGSize(width: value.location.x - value.startLocation.x, height: value.location.y - value.startLocation.y)
              }
              .onChanged { value in
                  print("I'm changing")
              }
              .onEnded { value in
                  print("I've ended")
              }
      )
    }
}

This approach is pretty helpful:这种方法非常有用:

Detect DragGesture cancelation in SwiftUI 检测 SwiftUI 中的 DragGesture 取消

So basically, you compose the drag gesture with pinch & rotation gestures(combine them using.simultaneously).所以基本上,你用捏合和旋转手势组成拖动手势(使用.simultaneously 组合它们)。

When either pinch or rotation gets triggered, you can use it to signal a cancellation for your drag gesture and update your state accordingly当触发捏合或旋转时,您可以使用它来发出取消拖动手势的信号并相应地更新您的 state

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM