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

发布于 2024-11-09 21:42:03 字数 52 浏览 5 评论 0原文

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

谢谢

How do I get the coordinates for finger tapping in UIView?
(I would prefer not to use a big array of buttons)

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赠意 2024-11-16 21:42:04

有两种方法可以实现此目的。如果您已经拥有正在使用的 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 进行子类化,不过,并且视图由视图控制器或其他任何东西拥有,那么您可以使用 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
    }
}

There’re two ways to accomplish this. If you’ve already got a subclass of UIView that you’re using, you can just override the -touchesEnded:withEvent: method on that subclass, like this:

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

If you’re not already subclassing UIView, though, and the view is owned by a view controller or whatever, then you can use a UITapGestureRecognizer, like this:

// 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
    }
}
夏了南城 2024-11-16 21:42:04

斯威夫特 3 答案

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
yourView.addGestureRecognizer(tapGesture)


func tapAction(_ sender: UITapGestureRecognizer) {

      let point = sender.location(in: yourView)


}

Swift 3 answer

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
yourView.addGestureRecognizer(tapGesture)


func tapAction(_ sender: UITapGestureRecognizer) {

      let point = sender.location(in: yourView)


}
纸短情长 2024-11-16 21:42:04

我假设你的意思是识别手势(和触摸)。开始寻找如此广泛的问题的最佳位置是Apple的示例代码 触摸。它遍历了大量的信息。

I assume you mean recognizing gestures (and touches). The best place to start looking for such a broad question is Apple's sample code Touches. It walks through a good amount of information.

書生途 2024-11-16 21:42:04
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:myView];
    NSLog("%lf %lf", touchPoint.x, touchPoint.y);
}

你需要做这样的事情。 touchesBegan:withEvent:UIResponder 的一种方法,UIViewUIViewController 均派生自该方法。如果你用谷歌搜索这个方法,你会发现几个教程。 Apple 的 MoveMe 示例是一个很好的示例。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:myView];
    NSLog("%lf %lf", touchPoint.x, touchPoint.y);
}

You need to do something like this. touchesBegan:withEvent: is a method of UIResponder from which UIView and UIViewController both are derived. If you google for this method then you will find several tutorials. MoveMe sample from Apple is a good one.

夕色琉璃 2024-11-16 21:42:04
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
    print("tap working")
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized
    { 
      `print(gestureRecognizer.locationInView(gestureRecognizer.view))`
    }
}
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
    print("tap working")
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized
    { 
      `print(gestureRecognizer.locationInView(gestureRecognizer.view))`
    }
}
桃扇骨 2024-11-16 21:42:04

Swift 5.6:

您可以在 UIResponder(UIView 和 UIViewController 继承自)中重写以下内容:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let point = touch.location(in: someView)
        print("x = \(point.x), y = \(point.y)")
    }
}

或者在手势识别器处理程序中:

@objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
    let point = gestureRecognizer.location(in: someView)
    print("x = \(point.x), y = \(point.y)")

}

Swift 5.6:

You can override the following in your UIResponder (which both UIView and UIViewController inherit from):

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let point = touch.location(in: someView)
        print("x = \(point.x), y = \(point.y)")
    }
}

Or in your gesture recognizer handler:

@objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
    let point = gestureRecognizer.location(in: someView)
    print("x = \(point.x), y = \(point.y)")

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文