如何从选中的位置获取CGPoint ?

时间:2023-02-05 20:35:11

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呢?

6 个解决方案

#1


46  

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

在这里,当您想要对您的全局对象或主视图的子视图进行某些操作时,此代码将使用

#2


19  

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,如果你想看到用户把手指从屏幕上移开的地方而不是触碰的地方。

#3


6  

it's probably better and simpler to use a UIGestureRecognizer with the map view instead of trying to subclass it and intercepting touches manually.

使用带有map视图的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):

第二步:接下来,实现shouldrecognizesimultaneouswithgesturerecognizer并返回YES,这样你的tap手势识别器就可以在地图的同时工作(否则点击图钉不会自动被地图处理):

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

#4


3  

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。

#5


3  

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

我想快速回答一下,因为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都将包含在视图中被触摸的点。

#6


0  

func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
    print("tap working")
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized { 
        `print(gestureRecognizer.locationInView(gestureRecognizer.view))`
    }
}

#1


46  

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

在这里,当您想要对您的全局对象或主视图的子视图进行某些操作时,此代码将使用

#2


19  

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,如果你想看到用户把手指从屏幕上移开的地方而不是触碰的地方。

#3


6  

it's probably better and simpler to use a UIGestureRecognizer with the map view instead of trying to subclass it and intercepting touches manually.

使用带有map视图的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):

第二步:接下来,实现shouldrecognizesimultaneouswithgesturerecognizer并返回YES,这样你的tap手势识别器就可以在地图的同时工作(否则点击图钉不会自动被地图处理):

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

#4


3  

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。

#5


3  

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

我想快速回答一下,因为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都将包含在视图中被触摸的点。

#6


0  

func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
    print("tap working")
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized { 
        `print(gestureRecognizer.locationInView(gestureRecognizer.view))`
    }
}