ios 横竖屏通知

  1. 屏幕切换时,会发送一个通知。只要注册一个通知:  
[java] view plaincopy
 
  1. [[NSNotificationCenter defaultCenter] addObserver:self   
  2.                                          selector:@selector(doRotateAction:)   
  3.                                              name:UIDeviceOrientationDidChangeNotification   
  4.                                            object:nil];  

然后在方法里做操作:

[java] view plaincopy
 
  1. -(void) doRotateAction:(NSNotification *) notification{  
  2.     if ([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortrait   
  3.         || [[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortraitUpsideDown) {   
  4.         NSLog(@">>>portrait");   
  5.     }else{   
  6.         NSLog(@">>>landscape");   
  7.     }  
  8. }  

如果要在入口文件做切换屏幕,可以判断状态栏的方向:

[java] view plaincopy
 
    1. ////////////////////////////////////  
    2. //通知委托状态栏已改变,进横竖屏操作  
    3. -(void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation{  
    4.     //清除背景,防止上一次转屏的图像残留  
    5.     [imageview setBackgroundColor:[UIColor clearColor]];  
    6.     //以下是横竖屏4个方向的切换,注意转屏时,无论是转哪个屏。起点坐标都是在portrait方向的起点(0,0)来计算的  
    7.     if ([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortrait ) {   
    8.         NSLog(@">>>portrait"); //home键在下  
    9.         imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 768, 44)];  
    10.         imageview.backgroundColor = [UIColor redColor];  
    11.         [_window addSubview:imageview];  
    12.     }else if([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortraitUpsideDown){  
    13.         NSLog(@">>>PortraitUpsideDown"); //home键在上  
    14.         imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 960, 768, 44)];  
    15.         imageview.backgroundColor = [UIColor redColor];  
    16.         [_window addSubview:imageview];  
    17.     }else if([[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft){  
    18.         NSLog(@">>>LandscapeLeft"); //home键在左  
    19.         imageview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 44, 1024)];  
    20.         imageview.backgroundColor = [UIColor redColor];  
    21.         [_window addSubview:imageview];  
    22.     }  
    23.     else if([[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeRight){   
    24.         NSLog(@">>>LandscapeRight"); //home键在右  
    25.         imageview = [[UIImageView alloc] initWithFrame:CGRectMake(704, 0, 44, 1024)];  
    26.         imageview.backgroundColor = [UIColor redColor];  
    27.         [_window addSubview:imageview];  
    28.     }  
    29.   
    30. }  
原文地址:https://www.cnblogs.com/piaojin/p/5083004.html