Search code examples
iosiphoneuikituipangesturerecognizer

iOS: What velocity threshold makes a pan gesture a flick?


In handling a UIPanGestureRecognizer in iOS, guidance such as that found here

https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_pan_gestures?language=objc

and

https://material.io/guidelines/patterns/gestures.html#gestures-drag-swipe-or-fling-details

advises using the velocity property to distinguish a normal drag from a swipe or a flick/fling. Nowhere does it say what a typical threshold is. For the sake of example, say we're dragging a thumbnail (44x44 points) across an iOS screen. Fine-tuning aside, above what velocity y-value would you consider the pan gesture to be a flick/fling?

Context: I'm trying to implement the iOS behavior you see in iOS 11 on an iPhone X, where swiping upward on the bar flings an app back to its home icon, except I'm doing it on cells being flung back to a UICollectionView.


Solution

  • After doing some research I found that Apple uses velocity of 300 to detect flicking in ScrollView.

    extension TestViewController: UIScrollViewDelegate {
        func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
            print(scrollView.panGestureRecognizer.velocity(in: view)) // if velocity > 300, UIScrollView will scroll to next page
        }
    }