The tilt to scroll functionality has become an integral part of any text heavy application. The first time we got introduced to the tilt to scroll functionality was through the Instapaper application. In this article we will demonstrate how to implement this functionality using Core Motion API.

What is Tilt to Scroll:

Tilt to scroll allows the user to scroll the content just by tilting the iPhone. This frees the user from manually scrolling the content.

Core Motion:

Starting in iOS 5 the didAccelerate method has been deprecated. This has been replaced by the Core Motion API. Core Motion allows you to capture many different motion units which includes accelerometer and Gyroscope. In this article we are only going to be interested in capturing accelerometer readings. This article requires a physical device since accelerometer cannot be used on a simulator.

Implementation:

Before implementation make sure that you have added a link to the Core Motion framework. Next you need to create a single instance of the CMMotionManager. We have added this in our AppDelegate implementation as shown below:



After that add a UIScrollView to the view and then add a UILabel to the UIScrollView. Make sure the label's "Lines" property is set to "0" and the "Line Breaks" is set t "Word Wrap". You will need to populate the label with some dummy text. We will simply accomplish this from reading text from file and assigning it to the label's text property.



The result is shown below:



Core Motion will capture the acceleration in x,y and z axis. We are only interested in the y axis acceleration. Not only that but we are also only interested in a particular angle or the arc generated by the tilt.

The following code makes sure that we only move the scroll view when the iPhone is tilted at a special angle:



Now, when you tilt your iPhone you will notice that the text start scrolling. If you tilt the iPhone more towards yourself then the text scrolls in the opposite direction.

Conclusion:

In this article we learned how to implement tilt to scroll functionality. This has become a very important feature for text content based applications.