【问题标题】:Getting the identifier of a touch获取触摸的标识符
【发布时间】:2017-06-02 11:52:48
【问题描述】:

我正在尝试在 touchesBegantouchesMovedtouchesEnded 中识别触摸。

在将UITouch(整个集合)打印到控制台时,我注意到开头有一个字符串,对于屏幕上的每个手指,它似乎都保持不变。

[<UITouch: 0x100d362e0> phase: ...

如何检索此属性?

【问题讨论】:

  • UITouch 是 NSObject 的子类。你可以访问它的属性并打印出来。
  • 0x100d362e0 是对象的内存地址(比较stackoverflow.com/questions/32797561/…),但我怀疑你是否需要它。你可以用===比较两个实例。
  • @TejasArdeshna 我知道如何访问属性。我似乎找不到包含我发布的代码 sn-p 的那个。
  • 为什么要识别触摸?如果两根手指靠得太近,硬件本身就无法做到这一点。

标签: ios swift multi-touch uitouch


【解决方案1】:

我不是 100% 确定您在问什么,但我认为您可以使用它作为跟踪唯一触摸实例的开始,但我不确定如果我们不创建触摸事件,它的可靠性有多高.

import UIKit

class ViewController: UIViewController {

    var touches: [UITouch] = []

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.touches.append(contentsOf: touches)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        touches.forEach {
            let index = self.touches.index(of: $0)
            print(String.init(describing: index))
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        touches.forEach {
            let index = self.touches.index(of: $0)
            print(String.init(describing: index))
        }
    }
}

【讨论】:

    【解决方案2】:

    为了识别 UITouch,我会使用哈希值

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        event?.allTouches?.forEach {
            print("touch began for: \($0.hash)")
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        event?.allTouches?.forEach {
            print("touch move for: \($0.hash)")
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        event?.allTouches?.forEach {
            print("touch end for: \($0.hash)")
        }
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        event?.allTouches?.forEach {
            print("touch cancelled for: \($0.hash)")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多