问题
In my app I will show settings, which only should be visible, if the device can 3D Touch. at the moment I check, if the device has ios9.
if #available(iOS 9.0, *)
problem is, for example the iPhone 6 have iOS 9 but no 3D Touch. how can I check the possibility of 3D Touch with swift?
i found this post, but no swift solution: Check if 3D touch is supported and enabled on the iOS9 device
One more Question. now i want to check if iOS 9 AND 3D Touch is available. i try this:
if (#available(iOS 9.0, *)) AND (is3DTouchAvailable() == true) {
But i always get this error:
Expected '{' after 'if' condition
回答1:
Try this swift code, self is referring to your UIViewController
func is3DTouchAvailable() -> Bool
{
return self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available
}
回答2:
Yes, if self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available will work, just make sure to wrap it up in a if #available(iOS 9, *)
So an example for Peek and Pop:
override func viewDidLoad()
{
super.viewDidLoad()
if #available(iOS 9, *) {
if (traitCollection.forceTouchCapability == .Available) {
if let collectionView = self.collectionView {
registerForPreviewingWithDelegate(self, sourceView: collectionView)
}
}
}
}
Moreover if you have an UITouch object (you can have it from method like touchesBegan, touchesMoved, ...) if your device is 3D Touch capable the touch.maximumPossibleForce property will be higher than 0. Don't forget to wrap it up in a if #available(iOS 9, *) neither ;)
Feel free checkout this Peek and Pop tutorial for more details, or this sample of 3D Touch code
来源:https://stackoverflow.com/questions/33686640/swift-check-if-3d-touch-is-possible