首页 > 解决方案 > 使用 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 机型

标签: iosswiftiphone3dtouch

解决方案


推荐阅读