Search code examples
iosuipangesturerecognizer

UIPanGestureRecognizer velocity in view could be nil?


In UIPanGestureRecognizer, there is this function to calculate the velocity of the gesture:

func velocity(in view: UIView?) -> CGPoint

The doc says:

view
The view in whose coordinate system the velocity of the pan gesture is computed.

I notice that view could be nil, which is a bit strange. Why and when should we use nil?


Solution

  • I believe it works the same way as location(in view: UIView?) does. If you don't pass a view object, then it'll calculate the velocity in window's coordinate system.

    location(in:)

    The view object in whose coordinate system you want the touch located. A custom view that is handling the touch may specify self to get the touch location in its own coordinate system. Pass nil to get the touch location in the window’s coordinates.