【问题标题】:UIView Partially Outside superView Not Receiving TouchesUIView 部分在 superView 之外没有接收到触摸
【发布时间】:2018-01-10 06:29:54
【问题描述】:

问题:viewWithGesture 包含 viewUserSees,并且可以在蓝色 containerView 中拖动。但是viewWithGesture是containerView的一个子View,所以当viewWithGesture处于极端时(这里说明——一半进一半出containerView),只有一半的viewWithGesture响应触摸,拖起来很吃力。

注意:我意识到我应该重做将它保存在容器中的所有数学运算并将其移到 containerView 之外,但我很想知道如何以“更糟糕”的方式做到这一点。

我对此进行了大量研究并尝试实现 hittest() 和 pointInside(),但到目前为止,我已经成功地让应用程序崩溃了。

有没有好的,相对干净的方式让用户从containerView外部抓取? (如果可能的话 swift3)

编辑:绿色框是透明的,一半在 containerView 中,一半不是。

【问题讨论】:

  • 左边的绿色矩形是什么?
  • 即在containerView之外的那一半viewWithGesture。我想现在使用透明度来说明它并没有那么热。

标签: ios swift3 uiview uigesturerecognizer


【解决方案1】:

为了让视图接收触摸,视图及其所有祖先必须从 pointInside:withEvent: 返回 true。

通常,pointInside:withEvent: 如果该点超出视图边界,则返回 false。由于绿色区域的触摸超出了容器视图的边界,因此容器视图返回 false,因此触摸不会触及手势视图。

要解决此问题,您需要为容器视图创建一个子类并覆盖其pointInside:withEvent:。在您的覆盖中,如果该点位于容器视图的边界或手势视图的边界内,则返回 true。也许你可以偷懒(特别是当你的容器视图没有很多子视图时)并且如果点在任何子视图的边界内就返回 true。

class ContainerView: UIView {

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if super.point(inside: point, with: event) { return true }
        for subview in subviews {
            let subviewPoint = subview.convert(point, from: self)
            if subview.point(inside: subviewPoint, with: event) { return true }
        }
        return false
    }

}

【讨论】:

  • 就是这样,很明显,hittest() 是一个陷阱,而且非常干净。也感谢您的良好解释,以前对此的其他答案似乎对有效的方法存在争议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多