简体   繁体   中英

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. However, if the user is swiping his fingers, then I know he started doing it with 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. 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

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. touchesMoved:withEvent: gets fired when one or more fingers in the event move. touchesEnded:withEvent: get fired when one or more of the fingers in the touch event are removed from the screen. Lastly, touchesCancelled:withEvent: is called if the whole event is invalidated (ie, cancelled).

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:

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.
  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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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