Currently I have Long Pres Gesture Recognizers on four different TableViews (two in each storyboard scene, therefore two storyboard scenes). I create these LPGR's 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 NSLog where the LPG was pressed...
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 it won't work, but I can't find a solution to get it to work. Instead of just returing one NSLog it returns something four times (once for each golfer because three of them have the indexPath=nil)
Any help will be appreciated. Also why is there such a lag for it to NSLog?
To determine the location of the gesture in the view from the recognizer’s locationInView: property:
// Get the location of the gesture
CGPoint location = [recognizer locationInView:self.view];
You can get the touch point of recognizer using ,
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
NSLog(@"%@",NSStringFromCGPoint([[gestureRecognizer valueForKey:@"_startPointScreen"] CGPointValue]));
}
you will get the point respect to the co-ordinate system of view for which your recognizer is added.
Your recognizer is registered only for the last Golfer. you should 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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With