1

For a data analysis project, I want to track the measurements for touch force, size and duration in a log-file for a simple app (I use the Foodtracker app from the Apple Documentation Website).

I know that I can get the force, size and duration from UITouch. But

  1. how do I access UITouch to get these measurements?
  2. and how do I write these measurements into a log-file?

1 Answers1

0

1. How do I access UITouch to get these measurements?

You'll have to override the touch handlers in your view controller.

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. How do I write these measurements into a log-file?

For that checkout this post: Read and write a String from text file

Lukas Würzburger
  • 6,543
  • 7
  • 41
  • 75
  • don't forget to call super when overriding those methods `super.touchesBegan(touches, with: event)` – Leo Dabus Mar 09 '19 at 11:55
  • Right. Even though it's only necessary if the view controller inherits from a class that is not `UIViewController` directly and also uses these methods. – Lukas Würzburger Mar 09 '19 at 11:58
  • Thanks! I works quite well, but not when I click on UIButtons. I also created and used a subclass of UIButtons and included the functions from above, but that did not work. What am I missing? – user11175222 Mar 09 '19 at 14:55
  • Well for that, please checkout this thread https://forums.developer.apple.com/thread/91263 – Lukas Würzburger Mar 09 '19 at 15:00