Swift Check If 3D Touch Is Possible

swift check if 3D Touch is possible

Try this swift code, self is referring to your UIViewController

func is3DTouchAvailable() -> Bool
{
return self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available
}

Check if 3D touch is supported and enabled on the iOS9 device

I was accidentally casting forceTouchCapability to a BOOL (using it as a return value to my method that was set to return a boolean). I needed to check if forceTouchCapability was equal to UIForceTouchCapabilityAvailable.

Instead of:

return [[MyView traitCollection] forceTouchCapability];

I need:

return [[MyView traitCollection] forceTouchCapability] == UIForceTouchCapabilityAvailable;

How to check 3D touch available in iPhone programmatically?

You should try this.

//check if the device supports 3DTouch
if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}else{
[[[UIAlertView alloc] initWithTitle:@"Error" message:@"3DTouch not available" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
}

3D touch/Force touch implementation

You can do it without a designated gesture recognizer. You do not need to adjust the touchesEnded and touchesBegan method, but simply the touchesMoved to obtain the correct values. getting the force of a uitouch from began/ended will return weird values.

UITouch *touch = [touches anyObject];

CGFloat maximumPossibleForce = touch.maximumPossibleForce;
CGFloat force = touch.force;
CGFloat normalizedForce = force/maximumPossibleForce;

then, set a force threshold and compare the normalizedForce to this threshold (0.75 seems fine for me).



Related Topics



Leave a reply



Submit