繁体   English   中英

在Swift中,如何在触发touchesMoved之后检查用户是否不再触摸屏幕?

[英]In Swift, how can I check that a user is no longer touching the screen after touchesMoved is triggered?

我需要知道用户开始触摸屏幕,然后知道何时不再触摸屏幕。

通过touchesBegantouchesEnded我可以获得此类信息。 但是,如果用户在滑动手指,那么我知道他是从touchesMoved开始的。
但是,我无法检查他何时不再触摸屏幕。 基本上,在用户停止滑动后, touchesEnded不会触发。 有任何想法吗?

我的代码示例:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


    if let touch = touches.first {
    print("Finger touched!")
    }
    super.touchesBegan(touches, withEvent:event)
}


override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {

        print("Finger is swipping!")

    }
    super.touchesBegan(touches, withEvent:event)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {

        print("finger is not touching.") //this will not fire if finger stopped swipping

    }
    super.touchesBegan(touches, withEvent:event)
}

你说:

print("finger is not touching.") //this will not fire if finger stopped swipping

没错,如果手指停止移动它不会触发。 但是,如果手指离开屏幕(停止触摸)火,这是你问有关(“不再触摸屏”)。

在Swift 3中工作

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if touches.first != nil {
        print("Finger touched!")
    }

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if touches.first != nil {
        print("finger is not touching.")  
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if touches.first != nil {
        print("Touch Move")
    }
}

touchesBegan:withEvent:触摸同一屏幕事件的一部分时,总是需要一个或多个手指被触摸。 touchesMoved:withEvent:事件中的一根或多根手指移动时会触发。 touchesEnded:withEvent:当触摸事件中的一根或多根手指从屏幕上移开时会被触发。 最后,如果整个事件都无效(即取消),则调用touchesCancelled:withEvent:

对于给定的事件,您将始终会收到一个或多个touchesBegan:withEvent:调用,可能会多次调用touchesMoved:withEvent ,然后会多次调用touchesEnded:withEvent:touchesCancelled:withEvent:

这里要考虑的几件事:

  1. 即使不执行任何操作,也应始终实现所有这四个方法,以防止部分事件沿响应者链上升(这是超级实现的作用)。
  2. 如果您正在处理事件,则不应调用super。
  3. 如果因为要在响应者链上调度事件而调用super,则必须调用正确的super方法(您在touchesMoved:withEvent实现中的super上调用touchesBegan:withEvent:

要具体回答您的问题,您可以实现touchesEnded:withEvent来了解用户何时停止通过事件中的一次或多次触摸来停止触摸屏幕。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM