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
Transforming a Cashapelayer to a Specific Size in Swift
Swift - Convert Values in Array to Doubles or Floats
How to Get All Keys and Values into Separate String Arrays from Nsdictionary in Swift
In iOS Avplayer, Addperiodictimeobserverforinterval Seems to Be Missing
How to Download a PDF File in Swift and Find in File Manager
How to Set Uitextview's Height Dynamically in Uitableviewcell Based on String Size
Periodically Call an API with Rxswift
Uidropinteractiondelegate Performdrop Not Called
How to Read Heart Rate from iOS Healthkit App Using Swift
Skspritenode Does Not Let Touches Pass Through When Isuserinteractionenabled False
How to Remove the Black Border of a Textfield
Two Uibezierpaths Intersection as a Uibezierpath
Calculate Distance Between My Location and a Mapkit Pin on Swift