首页 > 解决方案 > iOS 按钮与自定义键盘上的边缘手势冲突

问题描述

我在屏幕左侧和右侧的自定义键盘上有具有 .touchdown 操作的按钮。当我单击它们时,它们不会立即注册 .touchdown 并且似乎在等待查看是否有滑动手势。我尝试使用代码来推迟边缘手势,但在 iOS 13 中,这似乎不起作用。有没有人解决如何让边缘上的按钮在单击时立即执行 .touchdown 动作?

override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge {
    return [.left, .bottom, .right]
}

我还尝试在自定义按钮类中使用点函数,但这只会使其突出显示,但仍不能立即执行操作。

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    let inside = super.point(inside: point, with: event)

    if inside && !isHighlighted && event?.type == .touches {
        isHighlighted = inside
    }

    return bounds.insetBy(dx: -2.7, dy: -12).contains(point)
}

标签: iosxcodekeyboarduigesturerecognizer

解决方案


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let window = view.window,
        let recognizers = window.gestureRecognizers {
        recognizers.forEach { r in
            r.delaysTouchesBegan = false
            r.cancelsTouchesInView = false
            r.isEnabled = false
        }
    }
}

推荐阅读