[英]How can I make a CGRect move randomly around the screen so long as the user is touching it?
[英]In Swift, how can I check that a user is no longer touching the screen after touchesMoved is triggered?
我需要知道用户开始触摸屏幕,然后知道何时不再触摸屏幕。
通过touchesBegan和touchesEnded我可以获得此类信息。 但是,如果用户在滑动手指,那么我知道他是从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:
这里要考虑的几件事:
touchesMoved:withEvent
实现中的super上调用touchesBegan:withEvent:
要具体回答您的问题,您可以实现touchesEnded:withEvent
来了解用户何时停止通过事件中的一次或多次触摸来停止触摸屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.