如何实现在SwiftUI中触发开关情况的左右DragGesture()?]

问题描述 投票:0回答:1

我已经在视图中创建了一个DragGesture,无论用户向左或向右滑动,该视图都应选择@State(Bool)。

事实是只检测到向右滑动。

如何使用.gesture()捕获用户是在屏幕上向左还是向右滑动?

import SwiftUI

struct SwiftUIView: View {

  //MARK: Value to change on swipe gesture
  @State var swipeRight: Bool

  var body: some View {
    VStack {
      //MARK: Value displayed after user swiped
      Text($swipeRight ? "Right" : "Left")
    }
    .gesture(
      DragGesture()
        .onChanged { (value) in
          //MARK: What is it missing here?
          switch value.location.x {
          case ...(-0.5):
            self.swipeRight = false
            print("Swipe Left return false")
          case 0.5...:
            self.swipeRight = true
            print("Swipe Right return true")
          default: ()
          }
    })
}

我已经在视图中创建了一个DragGesture,无论用户向左或向右滑动,该视图都应选择@State(Bool)。事实是,仅检测到向右滑动。如何使用.gesture()捕获...

ios swift swiftui gesture
1个回答
0
投票

您应该比较旧位置和新位置:

© www.soinside.com 2019 - 2024. All rights reserved.