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

5

10

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.

Phagocytosis answered 7/11, 2012 at 12:45 Comment(0)
M
18

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]; 
Mathura answered 7/11, 2012 at 13:8 Comment(0)
B
4

When you link an UIPanGestureRecognizer to an IBAction, the action will get called on every change. The gesture recognizer also provides a property called state which indicates if it's the first UIGestureRecognizerStateBegan, the last UIGestureRecognizerStateEnded or just an event between UIGestureRecognizerStateChanged.

To solve your problem, try it like the following:

- (IBAction)panGestureMoveAround:(UIPanGestureRecognizer *)gesture {
    if ([gesture state] == UIGestureRecognizerStateBegan) {
        myVarToStoreTheBeganPosition = [gesture locationInView:self.view];
    } else if ([gesture state] == UIGestureRecognizerStateEnded) {
       CGPoint myNewPositionAtTheEnd = [gesture locationInView:self.view];
       // and now handle it ;)
    }
}

You may also have a look at the method called translationInView:.

Bacolod answered 7/11, 2012 at 13:8 Comment(0)
P
2

If you don't want to store anything you can also do this :

let location = panRecognizer.location(in: self)
let translation = panRecognizer.translation(in: self)
let previousLocation = CGPoint(x: location.x - translation.x, y: location.y - translation.y)
Pacheco answered 15/3, 2019 at 16:8 Comment(0)
V
0

You should instantiate your pan gesture recognizer as follows:

UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];

Then you should add panRecognizer to your view:

[aView addGestureRecognizer:panRecognizer];

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:

CGPoint point = [recognizer locationInView:aView];

You can also get the state of the panRecognizer:

if (recognizer.state == UIGestureRecognizerStateBegan) {
    //do something
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
   //do something else
}
Viable answered 7/11, 2012 at 13:10 Comment(0)
P
0

There is a function in UITouch to get the previous touch in the view

  • (CGPoint)locationInView:(UIView *)view;
  • (CGPoint)previousLocationInView:(UIView *)view;
Pacific answered 26/12, 2014 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.