How to determine the direction of UIPanGestureRecognizer dragging
Recently, you need to use UIPanGestureRecognizer to create a slide menu. You must not drag the menu to the right (click the button to slide right), but you can drag it back to the left; so you need to determine the direction of the drag. Baidu has a bit of it. Most of the answers on the Internet are as follows:
CGPoint translation = [recognizer translationInView: recognizer. view];
If (translation. x> 0)
{
// Slide to the right
}
Else
{
// Slide left
}
However, in actual development, I found that whether to drag to the left or to the right, translation. x is always greater than 0, so let's take a look at translationInView: the official explanation is to get the moving distance of a gesture on the relative specified view, that is, the pixel that moves on the X and Y axes, therefore, the velocityInView method is used to obtain the movement speed of the gesture in the specified view coordinate system. The result shows that the speed is in the direction,
CGPoint velocity = [recognizer velocityInView: recognizer. view];
If (velocity. x> 0)
{
// Slide to the right
}
Else
{
// Slide left
}
The problem is solved.