iosobjective-cxcodecore-graphics

Is there the equivalent of "fling" detection (Android) with UIGestureRecognizer on iOS?


Basically, fling designates the "energy" to apply after the finger is released from the screen after having swiped around (i.e. a panning event such as UIPanGestureRecognizer). It's like you swipe and then "throw" the object you have moved around: you get the force at which it has been thrown in the direction.

It is useful for me to apply inertia after a panning event on a scroll view (from my own custom framework, not using UIScrollView). Is there any way to get the equivalent on iOS? I don't really understand how it is calculated. It is not giving good results to use the last translation from the UIPanGestureRecognizer as a value for inertia.


Solution

  • You are looking for velocity(in:), which gives you points per second at the moment of measurement (which should be the moment of the gesture's .ended). What I do is convert this "momentum" into a spring animation's initialVelocity to bring the motion gently to a halt.

    Alternatively, depending on what you're trying to do here, you might find UIKit Dynamics a better fit for your use case.