简体   繁体   English

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

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

I need to know that a user started to touch the screen and then know when he is not touching it anymore. 我需要知道用户开始触摸屏幕,然后知道何时不再触摸屏幕。

With touchesBegan and touchesEnded I can get such information. 通过touchesBegantouchesEnded我可以获得此类信息。 However, if the user is swiping his fingers, then I know he started doing it with touchesMoved . 但是,如果用户在滑动手指,那么我知道他是从touchesMoved开始的。
However I am not able to check when he is no longer touching the screen. 但是,我无法检查他何时不再触摸屏幕。 Basically , touchesEnded will not fire after the user stopped swipping. 基本上,在用户停止滑动后, touchesEnded不会触发。 Any ideas? 有任何想法吗?

Example of my code: 我的代码示例:

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)
}

You said: 你说:

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

You're right that it will not fire if the finger stopped moving . 没错,如果手指停止移动它不会触发。 But it will fire if the finger leaves the screen (stops touching), which is what you asked about ("no longer touching the screen"). 但是,如果手指离开屏幕(停止触摸)火,这是你问有关(“不再触摸屏”)。

working in Swift 3 在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: always gets called for one or more fingers when they touch the screen that are considered part of the same touch event. touchesBegan:withEvent:触摸同一屏幕事件的一部分时,总是需要一个或多个手指被触摸。 touchesMoved:withEvent: gets fired when one or more fingers in the event move. touchesMoved:withEvent:事件中的一根或多根手指移动时会触发。 touchesEnded:withEvent: get fired when one or more of the fingers in the touch event are removed from the screen. touchesEnded:withEvent:当触摸事件中的一根或多根手指从屏幕上移开时会被触发。 Lastly, touchesCancelled:withEvent: is called if the whole event is invalidated (ie, cancelled). 最后,如果整个事件都无效(即取消),则调用touchesCancelled:withEvent:

For a given event you will always receive one or more calls to touchesBegan:withEvent: , probably many calls to touchesMoved:withEvent , and then either one or more calls to touchesEnded:withEvent: or touchesCancelled:withEvent: 对于给定的事件,您将始终会收到一个或多个touchesBegan:withEvent:调用,可能会多次调用touchesMoved:withEvent ,然后会多次调用touchesEnded:withEvent:touchesCancelled:withEvent:

A few things to consider here: 这里要考虑的几件事:

  1. You should always implement all four of these methods, even if you do nothing, to prevent partial events from going up the responder chain (which is what the super implementation does). 即使不执行任何操作,也应始终实现所有这四个方法,以防止部分事件沿响应者链上升(这是超级实现的作用)。
  2. You should not call super if you are handling the events. 如果您正在处理事件,则不应调用super。
  3. If you do call super because you want to dispatch the events up the responder chain, you must call the correct super method (you are calling touchesBegan:withEvent: on super in your touchesMoved:withEvent implementation 如果因为要在响应者链上调度事件而调用super,则必须调用正确的super方法(您在touchesMoved:withEvent实现中的super上调用touchesBegan:withEvent:

To answer your question specifically, you implement touchesEnded:withEvent to know when the user has stopped touching the screen with one or more touches that are part of the event. 要具体回答您的问题,您可以实现touchesEnded:withEvent来了解用户何时停止通过事件中的一次或多次触摸来停止触摸屏幕。

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

相关问题 只要用户触摸CGRect,如何使它在屏幕上随机移动? - How can I make a CGRect move randomly around the screen so long as the user is touching it? 如何通过触摸 SwiftUI 中的屏幕任意位置来更改文本 - How Can I Change The Text By Touching Anywhere Of The Screen In SwiftUI (快速)如何通过触摸按钮在pageviewcontroller中更改视图 - (Swift) how can I change view in pageviewcontroller with touching button 如何在 Swift 中触摸 UILabel 后对其进行编辑? - How can I edit a UILabel upon touching it in Swift? 如果玩家触摸屏幕,如何检查每一帧? - How to check each frame if player is touching screen? 如何在swift中旋转屏幕后获得视图的高度? - How can I get the height of a view after screen rotation in swift? 如何使用UISwipeGestureRecognizer代替Swift和SpriteKit的touchesMoved? - How do I use UISwipeGestureRecognizer instead of touchesMoved with Swift and SpriteKit? 如何通过按按钮检查用户是否在正确的区域中? 迅速 - How can I check if user is in the correct region by pressing a button? Swift AWS Cognito Swift SDK如何检查用户是否已确认? - AWS Cognito Swift SDK How can I check if a user is confirmed? 触摸屏手指数(Swift 2.0) - Number of fingers touching screen (Swift 2.0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM