如何在Swift中的touchedMoved期间检测新视图?

时间:2018-08-31 09:50:12

标签: ios swift xcode

我想在我的手指将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
}

1 个答案:

答案 0 :(得分:0)

如果您的aViewbView是同一superview的后代,并且它们的框架不相交,则可以使用以下代码:

let touchLocation = touch.location(in: superview)
if aView.frame.contains(touchLocation) {
    // touch in aView
} else if bView.frame.contains(touchLocation) {
    // touch in bView
}