目前,我在四个不同的TableView上具有Long Pres手势识别器(每个 Storyboard 场景中两个,因此两个 Storyboard 场景中)。我在ViewDidLoad方法中使用以下代码创建了这些LPGR ...

//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

接下来,我有另一种方法想要在按下LPG的地方进行NSLog ...
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);

我知道为什么它不起作用,但是我找不到解决方案来使其起作用。它不仅会重现一个NSLog,还会返回四次(对于每个高尔夫球手一次,因为其中三个高尔夫球手的indexPath = nil)。

任何帮助将不胜感激。还有为什么它对NSLog会有这样的滞后呢?

最佳答案

您可以使用来获取识别器的接触点,

 -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
 NSLog(@"%@",NSStringFromCGPoint([[gestureRecognizer valueForKey:@"_startPointScreen"] CGPointValue]));

}

您将获得相对于添加了识别器的坐标系的点的尊重。

您的识别器仅针对最后一位高尔夫球手进行注册。你应该做这个,
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];

关于iphone - 确定长按手势识别器的位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10021714/

10-11 14:55