ios 如何从点击位置获取 CGPoint?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10556120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a CGPoint from a tapped location?
提问by jkolber
I'm working on a graphing calculator app for the iPad, and I wanted to add a feature where a user can tap an area in the graph view to make a text box pop up displaying the coordinate of the point they touched. How can I get a CGPoint from this?
我正在为 iPad 开发一个图形计算器应用程序,我想添加一个功能,用户可以在其中点击图形视图中的一个区域,以弹出一个文本框,显示他们触摸的点的坐标。我如何从中获得 CGPoint?
回答by Paras Joshi
you have two way ...
你有两种方法...
1.
1.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
here,you can get location with point from current view...
在这里,您可以从当前视图中获取带有点的位置...
2.
2.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];
here,this code use when you want to do somthing with your perticular object or subview of your mainview
在这里,当您想对您的特定对象或主视图的子视图执行某些操作时,将使用此代码
回答by Dima
Try This
尝试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// Get the specific point that was touched
CGPoint point = [touch locationInView:self.view];
NSLog(@"X location: %f", point.x);
NSLog(@"Y Location: %f",point.y);
}
You can use "touchesEnded" if you'd rather see where the user lifted their finger off the screen instead of where they touched down.
如果您更希望看到用户将手指从屏幕上抬起的位置而不是他们按下的位置,则可以使用“touchesEnded”。
回答by Rob Norback
Just want to toss in a Swift 4answer because the API is quite different looking.
只想抛出Swift 4 的答案,因为 API 看起来完全不同。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = event?.allTouches?.first {
let loc:CGPoint = touch.location(in: touch.view)
//insert your touch based code here
}
}
OR
或者
let tapGR = UITapGestureRecognizer(target: self, action: #selector(tapped))
view.addGestureRecognizer(tapGR)
@objc func tapped(gr:UITapGestureRecognizer) {
let loc:CGPoint = gr.location(in: gr.view)
//insert your touch based code here
}
In both cases loc
will contain the point that was touched in the view.
在这两种情况下loc
都将包含在视图中被触摸的点。
回答by Kupendiran iOS
it's probably better and simpler to use a UIGestureRecognizer with the map view instead of trying to subclass it and intercepting touches manually.
将 UIGestureRecognizer 与地图视图一起使用可能更好更简单,而不是尝试对其进行子类化并手动拦截触摸。
Step 1 : First, add the gesture recognizer to the map view:
第 1 步:首先,将手势识别器添加到地图视图中:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapGestureHandler:)];
tgr.delegate = self; //also add <UIGestureRecognizerDelegate> to @interface
[mapView addGestureRecognizer:tgr];
Step 2 : Next, implement shouldRecognizeSimultaneouslyWithGestureRecognizer and return YES so your tap gesture recognizer can work at the same time as the map's (otherwise taps on pins won't get handled automatically by the map):
第 2 步:接下来,实现 shouldRecognizeSimultaneouslyWithGestureRecognizer 并返回 YES 以便您的点击手势识别器可以与地图同时工作(否则地图不会自动处理点击引脚):
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Step 3 : Finally, implement the gesture handler:
第 3 步:最后,实现手势处理程序:
- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr
{
CGPoint touchPoint = [tgr locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate
= [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f",
touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}
回答by diederikh
If you use an UIGestureRecognizer
or UITouch
object you can use the locationInView:
method to retrieve the CGPoint
within the given view the user touched.
如果您使用UIGestureRecognizer
或UITouch
对象,则可以使用该locationInView:
方法检索CGPoint
用户触摸的给定视图中的 。
回答by user3108511
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
print("tap working")
if gestureRecognizer.state == UIGestureRecognizerState.Recognized {
`print(gestureRecognizer.locationInView(gestureRecognizer.view))`
}
}