Nit*_*ram 3 iphone objective-c uigesturerecognizer
嗨我想获得任何控制的触摸位置/点或任何触摸发生的地方.
为此,我实现了这个,但我没有得到正确的接触点.
// Create gesture recognizer, notice the selector method
UITapGestureRecognizer *oneFingerTwoTaps =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
oneFingerTwoTaps.delegate=self;
// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:1];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
[[self view] addGestureRecognizer:oneFingerTwoTaps];
Run Code Online (Sandbox Code Playgroud)
和
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint point= [touch locationInView:touch.view];
NSLog(@"Point - %f, %f",point.x,point.y);
NSLog(@"Touch");
return NO; // handle the touch
}
Run Code Online (Sandbox Code Playgroud)
当我试图击中任何UIButton,UIImage,UITableView它没有给我正确的命中点是否有任何我做错的事情?请帮我.谢谢.
您的代码会在其出现的视图中打印触摸的位置.因此,如果您触摸中间的50x100大小的按钮,它将打印"Point 25.0,50.0".
如果要查找UIScreen的触摸位置,则必须转换值:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint point = [touch locationInView:touch.view];
CGPoint pointOnScreen = [touch.view convertPoint:point toView:nil];
NSLog(@"Point - %f, %f", pointOnScreen.x, pointOnScreen.y);
NSLog(@"Touch");
return NO; // handle the touch
}
Run Code Online (Sandbox Code Playgroud)
或者只是立即获取窗口(屏幕)空间中的坐标:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint pointOnScreen = [touch locationInView:nil];
NSLog(@"Point - %f, %f", pointOnScreen.x, pointOnScreen.y);
NSLog(@"Touch");
return NO; // handle the touch
}
Run Code Online (Sandbox Code Playgroud)