Weight things using iPhone screen

68 Views Asked by At

Swift has UIForceTouchCapability class which allows to measure things' weight using iPhone screen with touch force parameter. This class is available for iPhones which have 3D touch capability. How's it possible to get the same abilities on recent iPhones (iPhone 11, etc.) ?

Here is the code I've been using so far for this feature

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"
}

Now it's not working on iPhone models, which don't support 3D touch

0

There are 0 best solutions below