iosswift3dtouch

3D touch/Force touch implementation


How can we implement 3D touch to check if the user taps on UIView or force touch on UIView?

Is there a way to do this with UIGestureRecognize or only with UITouch?


Solution

  • 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).