ios - 如何从点击的位置获取 CGPoint?

标签 ios xcode ipad touch cgpoint

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

最佳答案

你有两种方式......

1.

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

在这里,您可以从当前 View 获取点的位置...

2.

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

这里,当你想对你的特定对象或你的主视图的 subview 做一些事情时使用这段代码

关于ios - 如何从点击的位置获取 CGPoint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556120/

相关文章:

iOS:授予服务器 Twitter 访问权限?

ios - 在 XCode 中构建项目时,有没有办法显示自定义警告?

xcode - 如何更改 FileMerge 的字体/设置/首选项?

iPhone 和 iPad 无法通过蓝牙找到对方

ios - 设置调用 RemoteIO 音频单元呈现回调的速率

iPhone - 删除呈现的 View Controller

iphone - iPhone 打印应用程序

iphone - 在 iPad 上截取部分屏幕截图

ios - 我的每个应用程序都需要单独的分发证书和开发证书吗?

ios - Xcode:Swift - 如何根据执行环境声明具有不同值的变量/常量?