获取触摸标识符

Getting the identifier of a touch

我试图在 touchesBegantouchesMovedtouchesEnded 中识别触摸。

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

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

如何检索此 属性?

我不是 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: [=10=])
            print(String.init(describing: index))
        }
    }

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

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

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    event?.allTouches?.forEach {
        print("touch began for: \([=10=].hash)")
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    event?.allTouches?.forEach {
        print("touch move for: \([=10=].hash)")
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    event?.allTouches?.forEach {
        print("touch end for: \([=10=].hash)")
    }
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    event?.allTouches?.forEach {
        print("touch cancelled for: \([=10=].hash)")
    }
}