iphone - 如何获取 UIView 中手指点击的坐标?

标签 iphone coordinates

如何获取 UIView 中手指点击的坐标? (我不想使用一大堆按钮)

谢谢

最佳答案

有两种方法可以实现此目的。如果您已经拥有正在使用的 UIView 子类,则只需重写该子类上的 -touchesEnded:withEvent: 方法,如下所示:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *aTouch = [touches anyObject];
    CGPoint point = [aTouch locationInView:self];
    // point.x and point.y have the coordinates of the touch
}

如果您还没有子类化 UIView,并且该 View 由 View Controller 或其他任何东西拥有,那么您可以使用 UITapGestureRecognizer,如下所示:

// when the view's initially set up (in viewDidLoad, for example)
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[someView addGestureRecognizer:rec];
[rec release];

// elsewhere
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateRecognized)
    {
        CGPoint point = [recognizer locationInView:recognizer.view];
        // again, point.x and point.y have the coordinates
    }
}

关于iphone - 如何获取 UIView 中手指点击的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6113860/

相关文章:

mysql - 我需要一些关于数据库设计的想法,与存储坐标相关

iphone - 如何从 iOS 应用程序将视频上​​传到 YouTube?

ios - SWRevealViewcontroller UIbutton 操作首先单击不起作用,然后它工作正常

iphone - MKMapVIew - 在点击点放置 Pin

javascript - 如何捕获在 HTML 中调用的 onclick 事件?

javascript - 如何在javascript中实现形状内像素算法

iphone - 应用程序代码签名验证失败,iPhone

iphone - 一次播放多个声音?

javascript - 一个简单的移动短代码

android - 如何在不跟踪的情况下只获取一次用户位置?