如何跟踪多次触摸
How to track multiple touches
嘿,我有一段很好的代码被感动了,它是@Sam_M给我的。
我试图在移动时基本上跟踪多个手指的位置。就像 Finger 1 在这里,Finger 2 在那里。因此,从现在开始,它会打印当前在视图上的 fingers/touches 的数量和位置,但它不会分别跟踪两个单独的手指。我环顾了 Whosebug 上仅有的 3 个其他问题。但是 none 给了我一个很好的结果,我可以在 swift.
中实现
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: UITouch in event!.allTouches()! {
for (index,touch) in touches.enumerate() {
let ptTouch = touch.locationInNode(self.view)
print("Finger \(index+1): x=\(pTouch.x) , y=\(pTouch.y)")
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: UITouch in event!.allTouches()! {
for (index,touch) in touches.enumerate() {
let ptTouch = touch.locationInNode(self.view)
print("Finger \(index+1): x=\(pTouch.x) , y=\(pTouch.y)")
}
}
}
触摸是手指。一个手指就是一个触摸。您可以通过识别触摸来识别手指;特定手指的触摸将与 相同的对象 相同,只要该手指正在触摸屏幕。要获取特定 UITouch 的唯一标识符,请获取其内存地址,如下所示:
let uuid = String(format: "%p", theTouch)
您可以将每次触摸的唯一标识符存储在 属性 中。但是不要存储触摸本身; Apple 禁止它。
每个手指的触摸对象在屏幕上时将保持相同的内存地址。您可以通过将触摸对象的地址存储在一个数组中,然后与该数组进行比较以准确了解哪个手指在移动,从而在多点触控场景中跟踪各个手指。
var fingers = [String?](count:5, repeatedValue: nil)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
for touch in touches{
let point = touch.locationInView(self.view)
for (index,finger) in fingers.enumerate() {
if finger == nil {
fingers[index] = String(format: "%p", touch)
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesMoved(touches, withEvent: event)
for touch in touches {
let point = touch.locationInView(self.view)
for (index,finger) in fingers.enumerate() {
if let finger = finger where finger == String(format: "%p", touch) {
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
for touch in touches {
for (index,finger) in fingers.enumerate() {
if let finger = finger where finger == String(format: "%p", touch) {
fingers[index] = nil
break
}
}
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
guard let touches = touches else {
return
}
touchesEnded(touches, withEvent: event)
}
更新 swift 4
归功于@Klowne
var fingers = [UITouch?](repeating: nil, count:5)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
for touch in touches{
let point = touch.location(in: self.view)
for (index,finger) in fingers.enumerated() {
if finger == nil {
fingers[index] = touch
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
for touch in touches {
let point = touch.location(in: self.view)
for (index,finger) in fingers.enumerated() {
if let finger = finger, finger == touch {
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
for touch in touches {
for (index,finger) in fingers.enumerated() {
if let finger = finger, finger == touch {
fingers[index] = nil
break
}
}
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
guard let touches = touches else {
return
}
touchesEnded(touches, with: event)
}
*根据苹果的更新文档,现在可以在多点触摸序列中保留触摸,只要它们在序列结束时被释放
嘿,我有一段很好的代码被感动了,它是@Sam_M给我的。
我试图在移动时基本上跟踪多个手指的位置。就像 Finger 1 在这里,Finger 2 在那里。因此,从现在开始,它会打印当前在视图上的 fingers/touches 的数量和位置,但它不会分别跟踪两个单独的手指。我环顾了 Whosebug 上仅有的 3 个其他问题。但是 none 给了我一个很好的结果,我可以在 swift.
中实现override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: UITouch in event!.allTouches()! {
for (index,touch) in touches.enumerate() {
let ptTouch = touch.locationInNode(self.view)
print("Finger \(index+1): x=\(pTouch.x) , y=\(pTouch.y)")
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: UITouch in event!.allTouches()! {
for (index,touch) in touches.enumerate() {
let ptTouch = touch.locationInNode(self.view)
print("Finger \(index+1): x=\(pTouch.x) , y=\(pTouch.y)")
}
}
}
触摸是手指。一个手指就是一个触摸。您可以通过识别触摸来识别手指;特定手指的触摸将与 相同的对象 相同,只要该手指正在触摸屏幕。要获取特定 UITouch 的唯一标识符,请获取其内存地址,如下所示:
let uuid = String(format: "%p", theTouch)
您可以将每次触摸的唯一标识符存储在 属性 中。但是不要存储触摸本身; Apple 禁止它。
每个手指的触摸对象在屏幕上时将保持相同的内存地址。您可以通过将触摸对象的地址存储在一个数组中,然后与该数组进行比较以准确了解哪个手指在移动,从而在多点触控场景中跟踪各个手指。
var fingers = [String?](count:5, repeatedValue: nil)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
for touch in touches{
let point = touch.locationInView(self.view)
for (index,finger) in fingers.enumerate() {
if finger == nil {
fingers[index] = String(format: "%p", touch)
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesMoved(touches, withEvent: event)
for touch in touches {
let point = touch.locationInView(self.view)
for (index,finger) in fingers.enumerate() {
if let finger = finger where finger == String(format: "%p", touch) {
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
for touch in touches {
for (index,finger) in fingers.enumerate() {
if let finger = finger where finger == String(format: "%p", touch) {
fingers[index] = nil
break
}
}
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
guard let touches = touches else {
return
}
touchesEnded(touches, withEvent: event)
}
更新 swift 4
归功于@Klowne
var fingers = [UITouch?](repeating: nil, count:5)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
for touch in touches{
let point = touch.location(in: self.view)
for (index,finger) in fingers.enumerated() {
if finger == nil {
fingers[index] = touch
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
for touch in touches {
let point = touch.location(in: self.view)
for (index,finger) in fingers.enumerated() {
if let finger = finger, finger == touch {
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
for touch in touches {
for (index,finger) in fingers.enumerated() {
if let finger = finger, finger == touch {
fingers[index] = nil
break
}
}
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
guard let touches = touches else {
return
}
touchesEnded(touches, with: event)
}
*根据苹果的更新文档,现在可以在多点触摸序列中保留触摸,只要它们在序列结束时被释放