Locating long press gesture recognizer
I currently have Long Pres Gesture Recognizers on four different table views (two in each storyboard scene, so two storyboard scenes). I am creating these LPGRs with the following code in my ViewDidLoad method ...
//Add Long Press Gesture Reconizer
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1; //seconds
lpgr.delegate = self;
[self.GolferOne addGestureRecognizer:lpgr];
[self.GolferTwo addGestureRecognizer:lpgr];
[self.GolferThree addGestureRecognizer:lpgr];
[self.GolferFour addGestureRecognizer:lpgr];
//Done Adding Long Press Gesture Reconizer
Next, I have another method that I want to use in NSLog where LPG was clicked ...
CGPoint p = [gestureRecognizer locationInView:self.GolferOne];
NSIndexPath *indexPath = [self.GolferOne indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@"long press on table view but not on a row [Golfer One]");
else
NSLog(@"long press on table view at row %d [Golfer One]", indexPath.row);
//Golfer Two
p = [gestureRecognizer locationInView:self.GolferTwo];
indexPath = [self.GolferTwo indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@"long press on table view but not on a row [Golfer Two]");
else
NSLog(@"long press on table view at row %d [Golfer Two]", indexPath.row);
//Golfer Three
p = [gestureRecognizer locationInView:self.GolferThree];
indexPath = [self.GolferThree indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@"long press on table view but not on a row [Golfer Three]");
else
NSLog(@"long press on table view at row %d [Golfer Three]", indexPath.row);
//Golfer Four
p = [gestureRecognizer locationInView:self.GolferFour];
indexPath = [self.GolferFour indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@"long press on table view but not on a row [Golfer Four]");
else
NSLog(@"long press on table view at row %d [Golfer Four]", indexPath.row);
I know why this won't work, but I can't seem to find a solution to get it to work. Instead of just saving one NSLog, it returns something four times (once for each golfer, because three of them have indexPath = nil)
Any help would be appreciated. Also, why is there such a lag behind it in NSLog?
source to share
You can get the touch point of the recognizer using
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
NSLog(@"%@",NSStringFromCGPoint([[gestureRecognizer valueForKey:@"_startPointScreen"] CGPointValue]));
}
you will get a point of view on the coordinate system for which your recognizer is added.
Your recognizer is only registered for the last golfer. you have to do this,
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1; //seconds
[self.GolferOne addGestureRecognizer:lpgr];
[lgpr release];
lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1; //seconds
[self.GolferTwo addGestureRecognizer:lpgr];
[lgpr release];
source to share
-
As TheDeveloper said, the long press gesture is for the whole view.
-
As an aside, if you are configuring gestures for multiple views, I believe you need a separate gesturer for each view. Not relevant here, but I see a lot of guys wondering why the gesture they were trying to assign to multiple views only works for one.
-
With long press gestures, I suggest you check your sender.state == UIGestureRecognizerStateEnded or Started or whatever you are looking for. You will receive multiple events triggered for the same user interaction.
-
If you have a gesture in the view and you want to see, for example, if the user has released their finger for a certain subset, you can get the CGPoint for the release (via locationInView as Vignesh pointed out), you can also get the CGRect for a specific frame of the view. and then check if CGPoint is inside CGRect via CGRectContainsPoint ().
source to share