Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
tutorials:automatically_update_the_screen_orientation [2012/07/31 19:38]
27.32.136.240 Fixed incorrect code
tutorials:automatically_update_the_screen_orientation [2013/03/05 10:19] (current)
Line 1: Line 1:
 +====== Automatically update the screen orientation ======
 +
 +//Here's a very useful code-snippet from this [[http://forum.sparrow-framework.org/topic/interface-orientation-solution|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:
 +
 +<code objc>
 +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
 +{
 +    return YES;
 +}
 +</code>
 +
 +In the SPStage class, in it's initWithWidth: Height: method add this:
 +
 +<code objc>
 +[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 +[[NSNotificationCenter defaultCenter] addObserver:self
 +    selector:@selector(onOrientationDidChange:)
 +    name:@"UIDeviceOrientationDidChangeNotification" object:nil];
 +</code>
 +
 +and add another method in this class:
 +
 +<code objc>
 +-(void)onOrientationDidChange:(UIEvent *)event
 +{
 +    NSLog(@"%i", [UIDevice currentDevice].orientation);
 +}
 +</code>
 +
 +Whenever the device is now turned, the ''onOrientationDidChange''-method gets called, and you can update the screen accordingly within this method.
  
 
 
Powered by DokuWiki