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.
There is a function in UITouch to get the previous touch in the view
When you link an
UIPanGestureRecognizer
to an IBAction, the action will get called on every change. The gesture recognizer also provides a property calledstate
which indicates if it's the firstUIGestureRecognizerStateBegan
, the lastUIGestureRecognizerStateEnded
or just an event betweenUIGestureRecognizerStateChanged
.To solve your problem, try it like the following:
You may also have a look at the method called
translationInView:
.You should instantiate your pan gesture recognizer as follows:
Then you should add panRecognizer to your view:
The
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
method will be called while the user interacts with the view. In handlePan: you can get the point touched like this:You can also get the state of the panRecognizer:
If you don't want to store anything you can also do this :
You can use this:
Store previous location by setting current location if not found and adding current location everytime.