swift check if 3D Touch is possible
Asked Answered
B

2

3

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

Bryant answered 13/11, 2015 at 5:43 Comment(0)
C
6

Try this swift code, self is referring to your UIViewController

func is3DTouchAvailable() -> Bool
{
    return self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available
}
Concerning answered 13/11, 2015 at 6:7 Comment(3)
new question in my first postBryant
what is your project minimum deployment target?Concerning
Could reduce the method to return traitCollection.forceTouchCapability == .Available.Ovate
P
1

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

Parlance answered 3/1, 2016 at 22:2 Comment(1)
Hi Quentin, regarding your 4 answers in sequence please take a look hereAlarick

© 2022 - 2024 — McMap. All rights reserved.