如何使uipangeSturer识别器检查手指是否在按钮框架中?

发布于 2025-02-08 14:32:50 字数 1275 浏览 2 评论 0原文

我正在尝试制作一个应用程序,用户可以将手指拖到多个按钮的顶部,并为每个按钮获取一些操作。

一个类似的问题尝试使用cgrectcontainspoint(button.frame,point)它说它被button.frame.contains(point)替换,但这似乎对我不起作用。 的链接

这是照片到目前为止所做

var buttonArray:NSMutableArray!

override func viewDidLoad()
{
    super.viewDidLoad()
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureMethod(_:)))
    a1.addGestureRecognizer(panGesture)
    a2.addGestureRecognizer(panGesture)
}
@objc func panGestureMethod(_ gesture: UIPanGestureRecognizer) {
    
    
    if gesture.state == UIGestureRecognizer.State.began {
        buttonArray = NSMutableArray()
    }
    
    let pointInView = gesture.location(in: gesture.view)
    
    if !buttonArray.contains(a1!) && a1.frame.contains(pointInView) {
        buttonArray.add(a1!)
        a1Tapped(a1)
    }
    else if !buttonArray.contains(a2!) && a2.frame.contains(pointInView) {
        buttonArray.add(a2!)
        a2Tapped(a2)
    }

:代码确实运行良好,但是当我试图激活阻力时,什么都没有发生。有技巧吗?

I am trying to make an app where the user could drag a finger on top of multiple buttons and get some actions for each button.

There was a similar question from a while back but when I tried to use CGRectContainsPoint(button.frame, point) it said it was replaced with button.frame.contains(point) but this didn’t seem to work for me. Here is a link to the Photo

What I have done so far:

var buttonArray:NSMutableArray!

override func viewDidLoad()
{
    super.viewDidLoad()
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureMethod(_:)))
    a1.addGestureRecognizer(panGesture)
    a2.addGestureRecognizer(panGesture)
}
@objc func panGestureMethod(_ gesture: UIPanGestureRecognizer) {
    
    
    if gesture.state == UIGestureRecognizer.State.began {
        buttonArray = NSMutableArray()
    }
    
    let pointInView = gesture.location(in: gesture.view)
    
    if !buttonArray.contains(a1!) && a1.frame.contains(pointInView) {
        buttonArray.add(a1!)
        a1Tapped(a1)
    }
    else if !buttonArray.contains(a2!) && a2.frame.contains(pointInView) {
        buttonArray.add(a2!)
        a2Tapped(a2)
    }

The code did run fine but when I tried to activate the drag nothing happened. Any tips?

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

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

发布评论

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

评论(1

长不大的小祸害 2025-02-15 14:32:52

您想要类似我假设的系统键盘的行为吗? cgRectContainsPoint未弃用:请参见 noreflowl noreferrer”> docs 。在Swift中,它像frage.contains()一样。

在处理矩形和点时,您必须确保首先将两者都转换为同一坐标系统。为此,您可以使用uiview上的转换/从方法:请参见(文档)。

在您的情况下。就像以下功能一样(首先翻译按钮帧,然后检查点是否在内部):

func touchedButtonForGestureRecognizer(_ gesture: UIPanGestureRecognizer) -> UIView? {
    let pointInView = gesture.location(in: gesture.view)
    for (button in buttonArray) {
        let rect = gesture.view.convert(button.frame from:button.superview)
        if (rect.contains(pointInView)) {
          return button
        }
    }
    return nil
}

You want behavior similar to the system keyboard I assume? CGRectContainsPoint is not deprecated: See the docs. In Swift it's written like frame.contains().

When dealing with rects and points you have to make sure both are translated to the same coordinate system first. To do so you can use the convert to/from methods on UIView: See (the docs).

In your case smth. like the following should work (first translate button frames, then check if the point is inside):

func touchedButtonForGestureRecognizer(_ gesture: UIPanGestureRecognizer) -> UIView? {
    let pointInView = gesture.location(in: gesture.view)
    for (button in buttonArray) {
        let rect = gesture.view.convert(button.frame from:button.superview)
        if (rect.contains(pointInView)) {
          return button
        }
    }
    return nil
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文