Create A 3D Touch Custom Gesture Recognizer in Swift

20 Apr. 2016 Mobile App

Apple introduced 3D Touch with the launch of iPhone 6S and 6S Plus. It includes a Taptic Engine in the screen that detects pressure sensitivity by the user. This opens up various possibilities for UI/UX purposes. Quick actions (Apple calls it Peek and Pop) for applications can now be performed by utilizing this new layer of interactivity called 3D Touch.

3d touch gesture recognizer

3D Touch in iPhone 6S

Basically, 3D Touch is a feature that has a layer of microscopic sensors in a screen to detect the difference between a hard press and a light tap. It helps to activate various menus or functions for iOS apps. Actions can be taken right from the notifications or home screen, without having to actually open or navigate to a particular app. For instance, a Facebook status can be updated directly from your home screen.

3D Touch further reduces the number of taps and swipes required to execute a task on your phone. It is a simple shortcut that runs and executes multiple functions across different applications. If you want to develop a custom iOS app with 3D Touch gesture recognizer, you need to first learn about the Deep Press Gesture Recognizer library. But how exactly will you integrate 3D Touch in your application?

Mechanics of Deep Press Gesture Recognizer:

The DeepPressGestureRecognizer is an extension of UIGestureRecognizer that causes an action when the press is harder and for a longer period. It basically extends the UIGestureRecognizer, where you will need to add a bridging header for importing Import UIKit/UIGestureRecognizerSubclass.h. Once that is done, you can freely override touchesBegan, touchesMoved and touchesEnded. In DeepPressGestureRecognizer, the first method of the two called handleTouch() checks either:

  • If a deep press has not been recognized but the touch force is above the threshold, then manage that touch event as the starting of the deep touch gesture.
  • If a deep press has been recognized but the touch force falls below the threshold, then manage that touch event as the end of the deep touch gesture.

Now let us look at the following tutorial that provides an overview to create a 3D Touch custom gesture recognizer in Swift.

The syntax for creating a 3D touch gesture is similar to that of any other gesture recognizer. For example – for long press event, it can be implemented using the below code:

let button = UIButton(type: UIButtonType.System)

button.setTitle(“Button with Gesture Recognizer”, forState: UIControlState.Normal)

stackView.addArrangedSubview(button)

let deepPressGestureRecognizer = DeepPressGestureRecognizer(target: self,
action: “deepPressHandler:”,
threshold: 0.75)

button.addGestureRecognizer(deepPressGestureRecognizer)

The action has similar states as the other recognizers, so when it is the “Began” state, it means that the user’s touch’s force has passed through the screen.

func deepPressHandler(value: DeepPressGestureRecognizer)
{
if value.state == UIGestureRecognizerState.Began
{
print(“deep press begin”)
}
else if value.state == UIGestureRecognizerState.Ended
{
print(“deep press ends.”)
}
}

If you feel this code is too much, here’s an alternative – DeepPressable Protocol. This protocol extension helps to get the deep press recognizer just by putting “DeepPressable” in your class.

class DeepPressableButton: UIButton, DeepPressable
{

}

Now set the right action in setDeepPressAction():

let deepPressableButton = DeepPressableButton(type: UIButtonType.System)
deepPressableButton.setDeepPressAction(self, action: “deepPressHandler:”)

Unfortunately, Apple’s Taptic Engine doesn’t have a public API. So we need to create custom iOS development for delivering 3D touch gesture features for your mobile app.

Conclusion

Even if the Taptic Engine is not accessible, we can deliver a visual feedback to create a 3D Touch custom gesture recognizer for iOS. You can utilize this code to create a continuous force gesture recognizer. It is hoped that this post helps you to integrate 3D Touch information into a custom gesture recognizer easily.

Are you looking to integrate 3D touch feature for your iOS apps? Rishabh Software has helped many organizations develop customized consumer-facing iOS applications. Discover our approach for developing a custom iOS News App to deliver engaging user experience.

Get a Free ConsultationTalk to our experts to get the best suited iOS solution for your business

Follow Us
Subscribe to the Blog
Get a Free Consultation
Reach out to our team to get a free consultation for your next projectContact Us