【问题标题】:Swift: record touch force, size, durationSwift:记录触摸力度、大小、持续时间
【发布时间】:2019-03-09 11:14:34
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios swift iphone uitouch


    【解决方案1】:

    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

    【讨论】:

    • 在覆盖这些方法时不要忘记调用 super super.touchesBegan(touches, with: event)
    • 对。即使只有当视图控制器直接从不是UIViewController 的类继承并且也使用这些方法时才需要。
    • 谢谢!我工作得很好,但当我点击 UIButtons 时就不行了。我还创建并使用了 UIButtons 的子类,并包含了上面的函数,但这不起作用。我错过了什么?
    • 好吧,请查看此主题forums.developer.apple.com/thread/91263
    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多