代码之家  ›  专栏  ›  技术社区  ›  K.K.D

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

  •  0
  • K.K.D  · 技术社区  · 7 年前


    如何在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 回复  |  直到 7 年前
        1
  •  0
  •   Anton Filimonov    7 年前

    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
    }