我想在我的手指将aView移到bView的touchesMoved期间检测到新视图。我使用touchs.first进行检测,但是即使我的手指触摸(按入)bView也会显示aView。
如何检测我在touchedMoved中触摸(推动)哪个视图?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
print(touch.view) // return aView
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
print(touch.view) // return aView even I'm touching bView from aView during this method
}
答案 0 :(得分:0)
如果您的aView
和bView
是同一superview
的后代,并且它们的框架不相交,则可以使用以下代码:
let touchLocation = touch.location(in: superview)
if aView.frame.contains(touchLocation) {
// touch in aView
} else if bView.frame.contains(touchLocation) {
// touch in bView
}