What is the best way to get Drag Velocity?

1.2k views Asked by At

I was wondering how can one get DragGesture Velocity?

I understand the formula works and how to manually get it but when I do so it is no where what Apple returns (at least some times its very different).

I have the following code snippet

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)
     }
}

From above:

A will output something like 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 will output something like Calculated: 287.6736739736197

Note from A I am looking at the 2nd value in valuePerSecond which is the y velocity.

Depending on how you drag, the results will be either different or the same. Apple provides the velocity as a property just like .startLocation and .endLocation but unfortunately there is no way for me to access it (at least none that I know) so I have to calculate it myself, theoretically my calculations are correct but they are very different from Apple. So what is the problem here?

2

There are 2 answers

0
粉红豚Vip On

Just like this:

let sss = "\(value)"
//Intercept string
let start = sss.range(of: "valuePerSecond: (")
let end = sss.range(of: ")))")
let arr = String(sss[(start!.upperBound)..<(end!.lowerBound)]).components(separatedBy: ",")
print(Double(arr.first!)!)
0
Lukáš Kubánek On

Final Answer

There is now a built-in DragGesture.Value.velocity property available with iOS 17 and it seems to be back-deployed all the way back to iOS 13. So, there’s no need for using reflection anymore.

Original Answer

This is another take on extracting the velocity from DragGesture.Value. It’s a bit more robust than parsing the debug description as suggested in the other answer but still has the potential to break.

import SwiftUI

extension DragGesture.Value {
    
    /// The current drag velocity.
    ///
    /// While the velocity value is contained in the value, it is not publicly available and we
    /// have to apply tricks to retrieve it. The following code accesses the underlying value via
    /// the `Mirror` type.
    internal var velocity: CGSize {
        let valueMirror = Mirror(reflecting: self)
        for valueChild in valueMirror.children {
            if valueChild.label == "velocity" {
                let velocityMirror = Mirror(reflecting: valueChild.value)
                for velocityChild in velocityMirror.children {
                    if velocityChild.label == "valuePerSecond" {
                        if let velocity = velocityChild.value as? CGSize {
                            return velocity
                        }
                    }
                }
            }
        }
        
        fatalError("Unable to retrieve velocity from \(Self.self)")
    }
    
}