How to get a CGPoint from a tapped location?

坚强是说给别人听的谎言 提交于 2019-11-27 07:26:39
Paras Joshi

you have two way ...

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.

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

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.

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.

Step 1 : First, add the gesture recognizer to the map view:

 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):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer
    :(UIGestureRecognizer *)otherGestureRecognizer
{
   return YES;
}

Step 3 : Finally, implement the gesture handler:

- (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);
}

Just want to toss in a Swift 4 answer because the API is quite different looking.

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.

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.

user3108511
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
    print("tap working")
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized { 
        `print(gestureRecognizer.locationInView(gestureRecognizer.view))`
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!