Automatically update the screen orientation

Here's a very useful code-snippet from this forum thread. Thanks, Arend!

One great thing about the iDevices is that you can detect the orientation of the screen and rotate the content accordingly. Here is a way to do it in Sparrow.

In the ApplicationDelegate do this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    return YES;
}

In the SPStage class, in it's initWithWidth: Height: method add this:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(onOrientationDidChange:)
    name:@"UIDeviceOrientationDidChangeNotification" object:nil];

and add another method in this class:

-(void)onOrientationDidChange:(UIEvent *)event
{
    NSLog(@"%i", [UIDevice currentDevice].orientation);
}

Whenever the device is now turned, the onOrientationDidChange-method gets called, and you can update the screen accordingly within this method.

  tutorials/automatically_update_the_screen_orientation.txt · Last modified: 2013/03/05 10:19 (external edit)
 
Powered by DokuWiki