- 屏幕切换时,会发送一个通知。只要注册一个通知:
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(doRotateAction:)
- name:UIDeviceOrientationDidChangeNotification
- object:nil];
然后在方法里做操作:
- -(void) doRotateAction:(NSNotification *) notification{
- if ([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortrait
- || [[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortraitUpsideDown) {
- NSLog(@">>>portrait");
- }else{
- NSLog(@">>>landscape");
- }
- }
如果要在入口文件做切换屏幕,可以判断状态栏的方向:
- -(void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation{
-
- [imageview setBackgroundColor:[UIColor clearColor]];
-
- if ([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortrait ) {
- NSLog(@">>>portrait");
- imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 768, 44)];
- imageview.backgroundColor = [UIColor redColor];
- [_window addSubview:imageview];
- }else if([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortraitUpsideDown){
- NSLog(@">>>PortraitUpsideDown");
- imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 960, 768, 44)];
- imageview.backgroundColor = [UIColor redColor];
- [_window addSubview:imageview];
- }else if([[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft){
- NSLog(@">>>LandscapeLeft");
- imageview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 44, 1024)];
- imageview.backgroundColor = [UIColor redColor];
- [_window addSubview:imageview];
- }
- else if([[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeRight){
- NSLog(@">>>LandscapeRight");
- imageview = [[UIImageView alloc] initWithFrame:CGRectMake(704, 0, 44, 1024)];
- imageview.backgroundColor = [UIColor redColor];
- [_window addSubview:imageview];
- }
-
- }
ios 横竖屏通知
原文:http://www.cnblogs.com/piaojin/p/5083004.html