快速:记录触摸力,大小,持续时间

时间:2019-03-09 11:14:34

标签: ios swift iphone uitouch

对于一个数据分析项目,我想在一个简单应用程序的日志文件中跟踪触摸力,大小和持续时间的测量值(我使用Apple文档网站上的Foodtracker app)。

我知道我可以从UITouch获得forcesizeduration。但是

  1. 我如何访问UITouch以获得这些测量值?
  2. 以及如何将这些度量值写入日志文件?

1 个答案:

答案 0 :(得分:0)

1。如何访问UITouch以获得这些测量值?

您必须在视图控制器中覆盖触摸处理程序。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    let touch = touches.first!
    print("\(touch.force)")
    print("\(touch.majorRadius)")
    print("\(touch.timestamp)")
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    let touch = touches.first!
    print("\(touch.force)")
    print("\(touch.majorRadius)")
    print("\(touch.timestamp)")
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesCancelled(touches, with: event)
    let touch = touches.first!
    print("\(touch.force)")
    print("\(touch.majorRadius)")
    print("\(touch.timestamp)")
}

2。如何将这些测量值写入日志文件?

为此签出该帖子:Read and write a String from text file