[英]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. 通过touchesBegan和touchesEnded我可以获得此类信息。 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: 这里要考虑的几件事:
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.