This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
tutorials:auto-rotating_the_interface [2013/05/30 12:46] – [Auto-rotating a game] daniel | tutorials:auto-rotating_the_interface [2013/05/30 12:47] (current) – [Auto-rotating a game] daniel | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Auto-rotating a game ===== | ||
+ | //Note: If you're using the Scaffold project of Sparrow 1.3 or 2.x, auto-rotation is already prepared for you. Find out all about it [[manual: | ||
+ | |||
+ | If your game should support different orientations and be updated automatically when you rotate your device, that's easy to do. | ||
+ | |||
+ | As described [[using_landscape_mode|here]], | ||
+ | |||
+ | ==== Get notified of orientation changes ==== | ||
+ | |||
+ | First, tell iOS that you want to be notified when the device' | ||
+ | |||
+ | <code objc> | ||
+ | [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; | ||
+ | [[NSNotificationCenter defaultCenter] addObserver: | ||
+ | selector: | ||
+ | name: | ||
+ | object: | ||
+ | </ | ||
+ | |||
+ | ==== React on those changes ==== | ||
+ | |||
+ | Then add the following method to the same class: | ||
+ | |||
+ | <code objc> | ||
+ | - (void)onDeviceOrientationChanged: | ||
+ | { | ||
+ | UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; | ||
+ | UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)deviceOrientation; | ||
+ | | ||
+ | // don't remove this, even if you're not displaying the status bar! | ||
+ | // it has some positive side effects. | ||
+ | [[UIApplication sharedApplication] setStatusBarOrientation: | ||
+ | | ||
+ | // update your game contents here, as described above | ||
+ | NSLog(@" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Whenever the device is now turned, that method gets called, and you can update the screen accordingly. //(Note that '' | ||
+ | |||
+ | If you want to support just a subset of those orientations, | ||
+ | |||
+ | <code objc> | ||
+ | - (void)onDeviceOrientationChanged: | ||
+ | { | ||
+ | UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; | ||
+ | | ||
+ | if (deviceOrientation == UIDeviceOrientationPortrait || | ||
+ | deviceOrientation == UIDeviceOrientationPortraitUpsideDown) | ||
+ | { | ||
+ | // etc. | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | That should do it! |