ios - 使用 iPhone 屏幕称重
问题描述
Swift 具有UIForceTouchCapability
允许使用带有触摸力参数的 iPhone 屏幕测量物体重量的类。此类适用于具有 3D 触控功能的 iPhone。如何在最近的 iPhone(iPhone 11 等)上获得相同的功能?
这是到目前为止我一直在使用的代码
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first { // 1
print(touch.maximumPossibleForce )
print(touch.force)
if self.traitCollection.forceTouchCapability == UIForceTouchCapability.available { // 3
if touch.force >= touch.maximumPossibleForce { // 4
forceLabel.text = "100%+ force"
grammLabel.text = "385 grams"
} else {
let force = (touch.force / touch.maximumPossibleForce) * 100 // 5
let grams = force * 385 / 100 // 6
let roundGrams = Int(grams) // 7
forceLabel.text = "\(Int(force))% force" // 8
grammLabel.text = "\(roundGrams) gramms"
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
forceLabel.text = "0% force"
grammLabel.text = "0 grams"
}
现在它不适用于不支持 3D 触控的 iPhone 机型
解决方案
推荐阅读
- javascript - 我可以使用纯 JS 将任意音频转换为 FLAC 吗?
- ios - 如何不再保存Core Data中已经保存的数据?
- go - 我无法使用 Golang 发送发布请求?
- python - 是否有允许不定期调用 Lambda 的 AWS 资源?
- javascript - 迭代匹配元素以控制自定义光滑轮播
- mysql - 如何在 join 和 groupBy 查询中添加分页功能?
- facebook - 没有客户数据的 Facebook 离线转化
- javascript - 如何替换正则表达式匹配的字符串的最后一个字符(javascript)
- php - 如何从id中提取客户信息?
- php - 在 Laravel 项目的数据库中插入和更新主视频的问题