Mae Hong Son loop random scenery

Do you even 3D touch?

Jakub Hladík
Hornet
Published in
2 min readDec 2, 2015

--

With the release of iPhone 6 and iPhone 6S Apple introduced entirely new way to interact with your phone. iPhone now senses how much pressure you apply to the display. That enables us (developers) to add 3D Touch-enabled actions to apps.

I have to admit that this feature left me pretty meh after the keynote. I did not really care. A month later when I bought the 6S I did not even bother to try it. Then my friend showed me working 3D touch in his app and that truly amazed me. I wanted to have it on Hornet. Now.

I promptly dediced to add quick peeks (profile photo and username) and pops to the grid, search and inbox. It turned to be was much easier than I expected…

Register for 3D Touch

First register your UIViewController to participate with 3D Touch preview (the peek) and commit (the pop).

Register for 3D Touch events.

The line above registers the receiver for 3D Touch events of its UICollectionView. You do not have to bother unregistering. Its done automatically upon dealloc.

The Peek

Press lightly and it gives you peek into the content…

The first callback relates to the light press event — you are supposed to peek into the content here. As soon as the lightest press is recognized this method is called. As a developer you have 2 jobs to do here:

1) Set the previewingContext’s sourceRect.

By doing so you get a very nice fullscreen dynamic blur reacting to the pressure with only the target rectangle (UICollectionView’s cell in our case) remaining clear.

2) Return a UIViewController subclass that peeks into the content.

In our case we return a dead simple controller with a UIImageView and UILabel only.

Handle the peek event.

This method’s implementation differs slightly for 3D Touches in UITableView (our inbox case). You get the cell’s index by querying your UITableView:

and set the previewingContext’s sourceRect using:

The pop

…continue pressing and it pops you into the content itself.

The second callback is trigged by applying even more pressure to the peek that is shown in the previous step. Its implementation is even simpler! You just pretend a normal UICollectionView’s tap happened and act accordingly.

Handle the pop event.

Conclusion

On have to admit Apple did a really good job with designing its 3D Touch API. Such a great feature could be implemented with less than 20 lines of code including safety checks!

If you are still hesitating whether or not to spend time implementing 3D Touch totally go for it. It took me less time to make it happen on Hornet than to write this blogpost 🦄.

--

--