目前我在四个不同的TableView上有Long Pres Gesture Recognizers(每个故事板场景中有两个,因此有两个故事板场景)。我在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会有这样的滞后?
答案 0 :(得分:6)
您可以使用
获取识别器的触摸点 -(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];
答案 1 :(得分:6)
从识别器的locationInView:property:
确定视图中手势的位置// Get the location of the gesture
CGPoint location = [recognizer locationInView:self.view];
答案 2 :(得分:0)
长按识别器应用于整个视图。要使用NSLog“滞后”,您可以使用NSTimer。一种你可以得到你想要的结果的方法是alpha为零的按钮。当它们释放时(无论是1秒还是120秒),它会记录触摸。
答案 3 :(得分:0)
正如TheDeveloper所说,长按手势适用于整个视图。
另外,如果您为多个视图设置手势,我相信您需要为每个视图分别设置一个手势识别器。这里不相关,但我看到很多人都在想为什么他们试图分配给多个视图的手势只适用于一个。
通过长按手势,我建议您检查您的sender.state == UIGestureRecognizerStateEnded或者启动或者您正在寻找的任何内容。您将为一次用户互动触发多个事件。
如果您在视图上有手势,并且您想要查看,例如,如果用户在特定子视图上方释放他们的手指,则可以获取CGPoint以进行发布(通过如Vignesh指出的那样,您可以获取特定子视图框架的CGRect,然后通过CGRectContainsPoint()检查CGPoint是否在CGRect内部。