Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pan gesture: why need setTranslation to zero?

Tags:

ios

gesture

I add a pan gesture to a view, move the view while finger moved, but I found if I do not call recognizer.setTranslation(CGPointZero, inView: self.view), translation is not right. why ?

  @IBAction func handlePan(recognizer:UIPanGestureRecognizer) {

    let translation = recognizer.translationInView(self.view)
    recognizer.view!.center = CGPoint(x:recognizer.view!.center.x + translation.x,
      y:recognizer.view!.center.y + translation.y)
    recognizer.setTranslation(CGPointZero, inView: self.view)// this line must need, why?
...
}
like image 682
BollMose Avatar asked Apr 10 '15 10:04

BollMose


People also ask

What does pan gesture mean?

The pan gesture is used for detecting the movement of fingers around the screen and applying that movement to content, and is implemented with the PanGestureRecognizer class.

How to use pan gesture?

The user must press one or more fingers on a view while panning on the screen. A panning gesture is on continuous action when the user moves one or more fingers allowed (minimumNumberOfTouches) to enough distance for recognition as a pan.


1 Answers

I don't speak English well, but I think it may be enough to explain this.

A translation in UIPanGestureRecognizer stands for a vector from where you started dragging to your current finger location, though the origin of this vector is {0, 0}. So all you need to determine the distance you dragged is another point of this vector. You get this point by calling :

recognizer.translationInView(self.view)

Then this point helped you setting a new location of your view. But UIPanGestureRecognizer is indeed a "continuous" reporter, she will not forget the state after the last report. she didn't know that you have used up that part of translation(to re-locate your view), so the next time when "handlePan" is called, the translation is not calculated from previous location of your finger , it is from the original place where started your finger dragging!!

That's why you have to call:

recognizer.setTranslation(CGPointZero, inView: self.view)

every-time you used that translation to re-locate your view, as if you are telling the recognizer that you are going to start a new drag gesture.

like image 50
Guofeng Pan Avatar answered Sep 22 '22 23:09

Guofeng Pan