Joe*_*Joe 11 point uiviewcontroller uiview ios swift
我正在尝试开发代码部分,以检查我的tap是在视图内还是在视图外部,我尝试使用pointInside方法.如果A是主视图控制器而B是A的子视图,我怎样才能知道用户在B内部轻敲.
Cod*_*ent 16
Apple在Responder Chain中解释得非常好.您可以将以下功能添加到视图控制器:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let firstTouch = touches.first {
let hitView = self.view.hitTest(firstTouch.location(in: self.view), with: event)
if hitView === viewB {
print("touch is inside")
} else {
print("touch is outside")
}
}
}
Run Code Online (Sandbox Code Playgroud)
Giu*_*nza 12
在tapGestureRecognizer回调中,您可以使用该方法tapGesture.location(in: A)来获取A推荐系统的x,y位置.然后你可以使用B.frame.contains(location).
如果为true,则点击在B中.
根据您的要求,可以采用更简单的方法解决问题.例如,gestureRecognizer如果您只对B内部的触摸感兴趣,可以添加到B而不是A.
我不知道你的需求到底是哪一个.有了更多信息,我可以给你更好的建议.
| 归档时间: |
|
| 查看次数: |
7318 次 |
| 最近记录: |