【发布时间】:2020-07-15 01:07:04
【问题描述】:
我想知道如何获得DragGesture Velocity?
我了解该公式的工作原理以及如何手动获取它,但是当我这样做时,它不是 Apple 返回的地方(至少有时它非常不同)。
我有以下代码sn-p
struct SecondView: View {
@State private var lastValue: DragGesture.Value?
private var dragGesture: some Gesture {
DragGesture()
.onChanged { (value) in
self.lastValue = value
}
.onEnded { (value) in
if lastValue = self.lastValue {
let timeDiff = value.time.timeIntervalSince(lastValue.time)
print("Actual \(value)") // <- A
print("Calculated: \((value.translation.height - lastValue.translation.height)/timeDiff)") // <- B
}
}
var body: some View {
Color.red
.frame(width: 50, height: 50)
.gesture(self.dragGesture)
}
}
从上面:
A 将输出类似Value(time: 2001-01-02 16:37:14 +0000, location: (250.0, -111.0), startLocation: (249.66665649414062, 71.0), velocity: SwiftUI._Velocity<__C.CGSize>(valuePerSecond: (163.23212105439427, 71.91841849340494)))
B 将输出类似Calculated: 287.6736739736197
来自A 的注释我正在查看valuePerSecond 中的第二个值,即y velocity。
根据您的拖动方式,结果会有所不同或相同。 Apple 提供速度作为属性,就像 .startLocation 和 .endLocation 一样,但不幸的是我无法访问它(至少我不知道)所以我必须自己计算,理论上我的计算是正确的,但它们与苹果有很大不同。那么这里有什么问题呢?
【问题讨论】:
标签: swiftui gesture draggesture