Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining Location of A Long Press Gesture Recognizer

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?

like image 777
The Man Avatar asked Apr 05 '12 01:04

The Man


2 Answers

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];
like image 143
polarware Avatar answered Oct 19 '22 13:10

polarware


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];
like image 37
Vignesh Avatar answered Oct 19 '22 14:10

Vignesh