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

问题描述 投票:0回答:1

当我的手指将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
}
ios swift xcode
1个回答
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
}
© www.soinside.com 2019 - 2024. All rights reserved.