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

标签 iphone objective-c ios cocoa-touch cocoa

目前我在四个不同的 TableView 上有 Long Pres Gesture Recognizers(每个 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 有这样的滞后?

最佳答案

根据识别器的 locationInView: 属性确定手势在 View 中的位置:

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

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

相关文章:

ios - 如何在 iOS 8 的 UINavigationViewController 中更改 UINavigationBar 的大小?

ios - 将 CGRect 从 View 转换为 super View 不起作用

ios - Waxsim Xcode 4.4

iphone - 在 iPhone 上传输音频时是否可以减少背景噪音?

iphone - UITableViewCell 的 UITextField subview 成为第一响应者?

objective-c - 在 Objective-C 中,我们是否必须使用 self.var 或仅使用 var 来引用 self 的属性?

ios - 在 Swift 中初始化 UITableViewCells 的最佳实践

ios - 添加新字符串后对 UITableVIew 进行排序

iphone - 如何以编程方式增加 iPhone 音量

iOS 共享扩展未获取图像