我的UIView中有3个子视图,每个子视图都有UIPanGestureRecognizer设置。我通过子类化UIView并覆盖了hitTest和指向内部函数来增加了每个视图的接触面积:
let x = CGFloat(30)
let y = CGFloat(30)
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitFrame = bounds.insetBy(dx: -x, dy: -y)
return hitFrame.contains(point) ? self : nil
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let hitTestEdgeInsets = UIEdgeInsets(top: -x, left: -x, bottom: -x, right: -y)
let hitFrame = bounds.inset(by: hitTestEdgeInsets)
return hitFrame.contains(point)
}
问题是在视图重叠或它们太近的情况下,我希望根据平移方向设置优先级。因此,如果view1和view2重叠(或足够接近以进行hitTest)并且平移方向为左,则我想在view1上平移。如果平移方向正确,我想在view2上平移。
实现此目标的正确方法是什么?