zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    luogu P5325 Min_25筛
    P5468 [NOI2019]回家路线 斜率优化 dp
    退役了
    6.18 省选模拟赛 树 倍增 LCT
    导出excel时一个页面问题-X11GraphicsEnvironment
    2019首篇
    Glide:重新加载失败的问题
    Android上传图片的两种方式
    Bug:No mapping for GET /onepill//swagger-ui.html
    Android集成百度地图
  • 原文地址:https://www.cnblogs.com/piaojin/p/5083004.html
Copyright © 2011-2022 走看看