The accelerator in the iPhone and iPad can tell you the device orientation. But before that, we need to understand the two types of orientation: device orientation and interface orientation. The device orientation can be portrait, landscape, faceUp, faceDown, and so on. It reflects how the iPhone or iPad itself is currently positioned. The interface orientation, tells us how the screen is being shown. The bar that contains the device’s battery life and shows us the time is called the status bar. Interface orientation basically means the status bar orientation, which can be Portrait, PortraitUpsideDown, LandscapeLeft or LandscapeRight.

Device Orientation
[[UIDevice currentDevice] orientation]

Start notifications
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]

Receive notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)
name:@”UIDeviceOrientationDidChangeNotification” object:nil];

To retrieve the device orientation, we can call UIDevice currentDevice orientation. If we want to be notified of updates, we need to make use of the NSNotificationCenter. First we have to tell the device to begin generating device orientation notifications, then set up Notification Center to start listening for updates. When we want to stop the updates, we can simply call endGeneratingDeviceOrientationNotifications on the UIDevice currentDevice.

Detecting Shakes
To detect shakes in any iOS device, we first need a custom UIView to detect shakes in. After which, we call becomeFirstResponder on the shakeView in the viewWillAppear and viewWillDisappear methods, to ensure that the shakeView will be the first to be notified of such events. We need to then implement the method, motionEnded, and handle the shake within. This is how we can detect shakes.

Add [shakeView becomeFirstResponder] in viewWillAppear and viewWillDisappear

@implementation ShakingView
– (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ if ( event.subtype == UIEventSubtypeMotionShake ){
// Handle shake here
if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] ) [super motionEnded:motion withEvent:event];
} – (BOOL)canBecomeFirstResponder{ return YES; } @end