31

我正在为 iPad 开发一个图形计算器应用程序,我想添加一个功能,用户可以点击图形视图中的一个区域来弹出一个文本框,显示他们触摸的点的坐标。我怎样才能从中获得 CGPoint?

4

6 回答 6

47

你有两种方式...

1.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  UITouch *touch = [[event allTouches] anyObject];
  CGPoint location = [touch locationInView:touch.view];
}

在这里,您可以从当前视图获取位置...

2.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];

在这里,当您想对您的特定对象或主视图的子视图做某事时,使用此代码

于 2012-05-11T18:13:41.790 回答
21

试试这个

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

}

如果您希望查看用户将手指从屏幕上抬起的位置而不是触摸的位置,则可以使用“touchesEnded”。

于 2012-05-11T17:54:36.480 回答
9

只是想折腾一个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
    }
}

或者

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
}

在这两种情况下loc都将包含在视图中被触摸的点。

于 2017-11-05T06:12:52.563 回答
6

将 UIGestureRecognizer 与地图视图一起使用可能会更好,更简单,而不是尝试对其进行子类化并手动拦截触摸。

第 1 步:首先,将手势识别器添加到地图视图中:

 UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(tapGestureHandler:)];
 tgr.delegate = self;  //also add <UIGestureRecognizerDelegate> to @interface
 [mapView addGestureRecognizer:tgr];

第 2 步:接下来,实现 shouldRecognizeSimultaneouslyWithGestureRecognizer 并返回 YES,以便您的点击手势识别器可以与地图同时工作(否则地图不会自动处理引脚上的点击):

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

第 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);
}
于 2015-09-02T06:02:18.897 回答
3

如果您使用UIGestureRecognizerorUITouch对象,您可以使用该locationInView:方法CGPoint在用户触摸的给定视图中检索。

于 2012-05-11T17:54:45.787 回答
0
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
    print("tap working")
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized { 
        `print(gestureRecognizer.locationInView(gestureRecognizer.view))`
    }
}
于 2016-04-12T04:53:35.700 回答