问题
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)
}
回答1:
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").
回答2:
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")
}
}
回答3:
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 (i.e., 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:
- 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).
- You should not call super if you are handling the events.
- 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 yourtouchesMoved: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.
来源:https://stackoverflow.com/questions/33311437/in-swift-how-can-i-check-that-a-user-is-no-longer-touching-the-screen-after-tou