Uma rajendran Uma rajendran - 12 days ago 7
Objective-C Question

How to get current touch point and previous touch point in UIPanGestureRecognizer method?

I am new to iOS, I am using

UIPanGestureRecognizer
in my project. In which I have a requirement to get current touch point and previous touch point when I am dragging the view. I am struggling to get these two points.

If I use
touchesBegan
method Instead of using
UIPanGestureRecognizer
, I could get these two points by the following code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CGPoint previous=[[touches anyObject]previousLocationInView:self];
}


I need to get these two points in
UIPanGestureRecognizer
event fire method. How can I achieve this? please guide me.

Answer

You can use this:

CGPoint currentlocation = [recognizer locationInView:self.view];

Store previous location by setting current location if not found and adding current location everytime.

previousLocation = [recognizer locationInView:self.view];