xcode - 获取 onLongPressGesture 持续时间的值
问题描述
我想知道如何获得 LongPressGesture 的持续时间。
我的目标是能够根据这个 LongGesture 按比例修改变量。
我试图引入一个@State var,以便通过 .onChanged / .onEnded 方法获取值,但是这个返回 LongGesture 的布尔值
这是我的代码(不编译):
struct ContentView: View {
// Well, this var @State is a CGFloat because I think that the duration is of this type (based on the minimumDuration)
@State var timeLongGesture: CGFloat = 0
@State var value: Int = 1
var body: some View {
// Some stuff here
Text("Increase the value")
.onTapGesture { value += 1 }
.gesture(
LongPressGesture(minimumDuration: 0.4)
.onEnded { valueLongPress in
// Here the error, because the value of "valueLongPress" is a Bool (as the doc mentionned)
timeLongGesture = valueLongPress
})
我觉得这很棘手。因此,如果有人有任何想法,我会接受 :-) 感谢您的帮助。
解决方案
This is how I would get the duration of an LongPressGesture:
class InitialTime: ObservableObject {
@Published var initTime = Date()
}
struct ContentView: View {
@ObservedObject var kickoff = InitialTime()
@State var timeLongGesture: Double = 0.0
@State var value: Int = 1
@GestureState var isDetectingLongPress = false
var body: some View {
// Some stuff here
Text("Increase the value")
.onTapGesture { self.value += 1 }
.gesture(LongPressGesture(minimumDuration: 3)
.updating($isDetectingLongPress) { _, _, _ in
self.kickoff.initTime = Date()
print("--------> self.kickoff.initTime: \(self.kickoff.initTime)")
}
.onEnded { finished in
self.timeLongGesture = Date().timeIntervalSince(self.kickoff.initTime)
print("-----> timeLongGesture: \(self.timeLongGesture)")
})
}
}
推荐阅读
- javascript - 我可以使用实例的属性来更新其他属性吗?
- javascript - 我收到一个使用 javascript 的 API 的 404 HTTP 错误,但在我的终端中没有
- regex - 正则表达式捕获不返回预期的数组
- reactjs - 在 setInterval 中每 0.1 秒执行一次 setState 导致的渲染问题
- python - 使用 Python 列出来自 JIRA 的问题
- python - 关于 Python 中的类的初学者问题
- javascript - 如何让我的机器人在响应时不提及用户?
- reactjs - material-ui的Popper组件中使用箭头的例子
- swift - 如何重绘 NavigationBar 以在按下按钮时显示 SearchController?
- loops - 循环遍历 ansible dict 以获取特定值