确定长按手势识别器的位置

10

目前我在四个不同的TableView上拥有长按手势识别器 (每个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被按下的位置...

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?
4个回答

9

要确定手势在视图中的位置,可以使用识别器的locationInView:属性:

// Get the location of the gesture
CGPoint location = [recognizer locationInView:self.view];

这就是我一直在寻找的东西。比KVC好多了;-) - Katlu

6
您可以使用```UITapGestureRecognizer```获取识别器的触摸点。
 -(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];

但是在手册中,UIGestureRecognizer的"valueForKey"在哪里解释了呢? - gdm
valueForKey 是 KVC。KVC 可以应用于任何符合 KVC 的对象。 - Vignesh
我知道这篇文章有点老了,但对于未来遇到此问题的人来说仍然有用。如果您想确定哪一行被长按了,您需要使用CGPoint p = [gestureRecognizer locationInView:myTableView]。从键_startPointScreen中获取的值是偏移的,当使用NSIndexPath *indexPath = [myTableView indexPathForRowAtPoint:p]时会返回错误的行。 - Dave S

0

长按识别器应用于整个视图。要使用NSLog产生“滞后”,您可以使用NSTimer。一种获得所需结果的方法是使用透明度为零的按钮。当它们释放(无论是1秒还是120秒),它会记录触摸。


0
  1. 正如TheDeveloper所说,长按手势适用于整个视图。

  2. 另外,如果您要为多个视图设置手势,我相信您需要为每个视图单独设置一个手势识别器。虽然这在此处不相关,但我看到很多人会想知道为什么他们试图为多个视图分配的手势只适用于一个视图。

  3. 对于长按手势,建议您检查 sender.state == UIGestureRecognizerStateEnded 或 Started,或者您要查找的任何内容。您将获得多个事件触发一次用户交互。

  4. 如果您将手势放在视图上,而您想查看例如,用户是否在特定子视图上释放了手指,您可以通过locationInView获取释放的CGPoint(正如Vignesh所指出的那样),然后还可以获取特定子视图框架的CGRect,然后通过CGRectContainsPoint()检查CGPoint是否在CGRect内。


请注意,您将根据它们的坐标系获得点数。因此,在使用CGRectContainsPoint之前,必须有效地转换坐标系。 - Vignesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接