分享

监听iOS检测屏幕旋转状态,不需开启屏幕旋转

 勤奋不止 2014-07-07
  1. -(void)rotation_icon:(float)n {  
  2.       
  3.     UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>][self.view viewWithTagName:@"home_history"];  
  4.     UIButton *cam_btn = [self.view viewWithTagName:@"cam_btn"];    UIButton *cut_btn = [self.view viewWithTagName:@"cut_btn"];      UIButton *light_btn=[self.view viewWithTagName:@"light_btn"];  
  5.     history_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  6.     cam_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  7.     cut_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  8.     light_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  9. }  
  10. - (void)orientationChanged:(NSNotification *)note  {      UIDeviceOrientation o = [[UIDevice currentDevice] orientation];  
  11.     switch (o) {  
  12.         case UIDeviceOrientationPortrait:            // Device oriented vertically, home button on the bottom  
  13.             [self  rotation_icon:0.0];  
  14.             break;  
  15.         case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top  
  16.             [self  rotation_icon:180.0];  
  17.             break;  
  18.         case UIDeviceOrientationLandscapeLeft:      // Device oriented horizontally, home button on the right  
  19.             [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];  
  20.   
  21.             [self  rotation_icon:90.0*3];  
  22.             break;  
  23.         case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left  
  24.             [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];  
  25.   
  26.             [self  rotation_icon:90.0];  
  27.             break;  
  28.         default:  
  29.             break;  
  30.     }   
  31.  }  
  32.   
  33.   
  34. -(void)viewWillDisappear:(BOOL)animated {      
  35.   
  36.   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];  
  37.     UIDevice *device = [UIDevice currentDevice]; //Get the device object  
  38.     [nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device];  
  39. }  
  40.   
  41.  - (void)viewDidAppear:(BOOL)animated {   
  42.      // Do any additional setup after loading the view from its nib.         
  43.  //----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----  
  44.     UIDevice *device = [UIDevice currentDevice]; //Get the device object  
  45.     [device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation  
  46.     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app  
  47.     [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:device];  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约