首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在UIView中获取手指敲击的坐标?

如何在UIView中获取手指敲击的坐标?
EN

Stack Overflow用户
提问于 2011-05-25 00:43:21
回答 5查看 20.2K关注 0票数 8

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

谢谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-05-25 00:51:48

有两种方法可以做到这一点。如果您已经有了正在使用的UIView的一个子类,那么只需覆盖该子类上的-touchesEnded:withEvent:方法,如下所示:

代码语言:javascript
运行
复制
- (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的子类化,并且视图由视图控制器或其他任何东西拥有,那么您可以使用UITapGestureRecognizer,如下所示:

代码语言:javascript
运行
复制
// 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
    }
}
票数 35
EN

Stack Overflow用户

发布于 2017-08-16 21:04:24

Swift 3答案

代码语言:javascript
运行
复制
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
yourView.addGestureRecognizer(tapGesture)


func tapAction(_ sender: UITapGestureRecognizer) {

      let point = sender.location(in: yourView)


}
票数 3
EN

Stack Overflow用户

发布于 2011-05-25 00:47:10

我想你的意思是识别手势(和触摸)。要开始寻找这样一个广泛的问题,最好的地方是苹果的示例代码Touches。它浏览了大量的信息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6113860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档