Nate Weiner

This is an old archived post from my former blog The Idea Shower. It's where I cataloged my product explorations and releases, one of which ultimately became Pocket.

This post was published back in 2009. It may not function as originally intended or may be missing images.

One Touch Rotation Lock on the iPhone

September 14, 2009

1-3-rotateOne feature I get a lot of comments about in Read It Later Pro is the one touch rotation lock.  When the user rotates their phone, it displays a lock icon for a brief second.  If the user taps the lock, they can lock the rotation of the device so the view does not change.  This is so much easier/faster than making a user go back out to the options screen and setting a toggle.

Since a lot of users have said they wished other applications had this feature I thought I'd release the source code so that other developers could easily add it if they wish.

How to Use

Every app is going to be unique, so depending on how your application and it's views are designed, this may or not be a quick drop-in.

I would start first by downloading and viewing the example project.

There are two parts you would need to integrate into your application.

1. Add the view controller additions to your UIViewController

Open OneTouchRotationViewController.h and OneTouchRotationViewController.m.  You will see the additional properties and functions I've added to the UIViewController.  In most cases, you can simply copy and paste them into your main view controller.

The example project has no extraneous methods or code, so you should carry over almost all of the code from the two files into your own.

Make sure you get:

  • The definitions in the header (.h) file.
  • The #defined degreesToRadians function at the top of the .m file
  • The OneTouchRotation enum
  • The synthesized properties line
  • The hideLockAfterNumberOfSeconds constant
  • All of the source for the rotation functions listed in the .m file

2. Add the rotation notification

Where you put this may again depend on your application.  If you open OneTouchRotationAppDelegate.m, you'll see that I add it right when the application starts up.  This event tells your view controller when the device has been rotated.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:viewController selector:@selector(didRotate:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

3. Add the icon images

I provided the same icons I use in my own app.  Feel free to modify them if you wish.  You'll want to add these to your own project by dragging both rotateUnlock.png and rotateLocked.png into your Resources folder in Xcode.

Test it out

If you get stuck, start by taking a look at the example project included in the download.

Download

One Touch Rotation Example Project and Source (1.0)