蘋果提供一個觀察者這一點。這是一個例子。
File.h
#import <UIKit/UIKit.h>
@interface RotationAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
-(void)orientationChanged;
@end
File.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//Get the device object
UIDevice *device = [UIDevice currentDevice];
//Tell it to start monitoring rthe accelermeter for orientation
[device beginGeneratingDeviceOrientationNotifications];
//Get the notification center for the app
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
//Add yourself an observer
[nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
HeavyViewController *hvc = [[HeavyViewController alloc] init];
[[self window] setRootViewController:hvc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)orientationChanged:(NSNotification *)note
{
NSLog(@"OrientationChanged: %d", [[note object] orientation]);
//You can use this method to change your shape.
}