当我的手指将aView移动到bView时,我想在touchesMoved中检测到新视图。我使用touches.first检测它,但它显示aView甚至我的手指触摸(推)bView。 如何在touchMoved中检测我触摸(推动)的视图?
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
}
如果你的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
}