【问题标题】:How do I make a SwiftUI gesture that keeps running code while the view is pressed如何制作 SwiftUI 手势,在按下视图时继续运行代码
【发布时间】:2020-04-30 12:27:10
【问题描述】:

我正在尝试制作一个控制计数器的按钮。如果你点击它,计数器会加一。但是如果你点击并按住它,我希望计数器在你按住它的同时每 n 秒增加一个,并一直这样做直到你松开。

如果我使用如下代码:

@GestureState var isDetectingLongPress = false
var plusLongPress: some Gesture {
    LongPressGesture(minimumDuration: 1)
        .updating($isDetectingLongPress) { currentstate, gestureState, _ in
            gestureState = currentstate
        }
        .onEnded { finished in
            print("LP: finished \(finished)")
        }
}

然后isDetectingLongPress在一秒钟后变为真,然后立即变为假。 onEnded 中的打印也会在 1 秒后调用。

我想要一些方法来保持调用代码在手指按下视图时不断更新计数器 - 而不仅仅是在检测到长按后一次。

【问题讨论】:

  • 更新:我正在使用 LongPressGesture 和 DragGesture 进行排序 - 但我并没有真正拖动(手指不会移动),所以我没有收到更新电话。不过我想我可以启动一个计时器——如果我弄清楚了,我会发布一个解决方案。

标签: ios swift swiftui


【解决方案1】:

使用以下组合来跟踪连续按下

使用 Xcode 11.4 / iOS 13.4 测试

@GestureState var isLongPress = false // will be true till tap hold

var plusLongPress: some Gesture {
    LongPressGesture(minimumDuration: 1).sequenced(before:   
          DragGesture(minimumDistance: 0, coordinateSpace: 
          .local)).updating($isLongPress) { value, state, transaction in
            switch value {
                case .second(true, nil):
                    state = true
                   // side effect here if needed
                default:
                    break
            }
        }
}

【讨论】:

  • 我没有收到持续的回调,但如果你将它与 let timer = Timer.TimerPublisher(interval: 0.25, runLoop: .main, mode: .common).autoconnect().onReceive(timer) { _ in ... 结合使用,那么你可以在计时器中监控 isLongPress 并仅在它被持有时更新计数器跨度>
  • 为什么需要用拖拽手势来排序?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 2020-10-19
  • 1970-01-01
  • 2012-08-26
  • 2023-01-29
  • 1970-01-01
  • 2014-03-01
相关资源
最近更新 更多