The*_*Man 5 iphone cocoa cocoa-touch objective-c ios
目前我在四个不同的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
Run Code Online (Sandbox Code Playgroud)
接下来我有另一种方法,我想要按下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);
Run Code Online (Sandbox Code Playgroud)
我知道为什么它不起作用,但我找不到解决办法让它发挥作用.而不是仅仅撤回一个NSLog,它返回四次(每个高尔夫球手一次,因为其中三个有indexPath = nil)
任何帮助将不胜感激.另外为什么NSLog会有这样的滞后?
你可以使用识别器的接触点,
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
NSLog(@"%@",NSStringFromCGPoint([[gestureRecognizer valueForKey:@"_startPointScreen"] CGPointValue]));
}
Run Code Online (Sandbox Code Playgroud)
您将获得关于添加识别器的坐标系统的观点.
您的识别器仅在最后一个高尔夫球手上注册.你应该做这个,
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];
Run Code Online (Sandbox Code Playgroud)
要从识别器的locationInView:属性确定视图中手势的位置:
// Get the location of the gesture
CGPoint location = [recognizer locationInView:self.view];
Run Code Online (Sandbox Code Playgroud)