Ilya
Ilya

Reputation: 151

UIPanGestureRecognizer translationInView vs locationInView

I'm taking the first steps in Swift

What is the best method to move UIView along X coordinate for UIPanGestureRecognizer?

Usually Programmers use translationInView for pan gesture

But I suppose the locationInView is more convenient for this purpose

Upvotes: 0

Views: 2065

Answers (2)

andrewcar
andrewcar

Reputation: 422

translationInView provides the change in coordinates that the view has moved within the view it is contained in. That is given in the form of a CGPoint.

If you drag it to the left, it might provide the coordinates:

(-20.0, 0)

locationInView provides the location of the view in the form of a CGPoint.

If you drag it to the left and print the locationInView throughout the gesture, you will see a slur of logs with changing coordinates with the final one being its current resting place.

To move a UIView with the touch's location, you would want to set the frame property of the UIView to the gesture's locationInView in the gesture's delegate method.

Upvotes: 3

Sulthan
Sulthan

Reputation: 130122

A pan gesture is composed from individual touches on the screen, locationInView gives you the current position of the touch on the screen, translationInView gives you the movement relative to the previous touches.

When panning, you are usually interested more in the movement (e.g. when scrolling, you don't want to know where the scroll began and where it ended) and not in the current position. If you are using the pan recognizer for example for drag and drop, then the location will interest you in the final position (you want to know where to drop).

Upvotes: 1

Related Questions