zoukankan      html  css  js  c++  java
  • ios单独的页面支持横竖屏的状态调整,HTML5加载下(更新2)

    单独的页面支持横竖屏的状态调整,HTML5加载下

    工程中设置只支持竖屏状态,在加载HTML5的界面可以是横竖屏的,在不对工程其他界面/设置做调整的同时,可以这样去

    #import "ViewController.h"
    
    #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
    
    #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
    
    @interface ViewController ()<UIWebViewDelegate>
    {
        UIWebView *webview;
        
        
        
        UIButton * back;
    
        
    }
    
    
    @end
    
    
    @implementation ViewController
    
    
    //此状态一定要是 NO  不然无法对旋转后的尺寸进行适配
    -(BOOL)shouldAutorotate{
        
        return NO;
    }
    //支持的状态
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        // 如果该界面需要支持横竖屏切换
        return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
      
    }
    
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        
    //    self.tabBarController.tabBar.hidden = YES;
        
    //    self.navigationController.navigationBar.hidden= NO;
    //    
    //    
    //    //隐藏 状态栏
    //    [[UIApplication sharedApplication]setStatusBarHidden:YES];
    //    
        
    }
    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
    //    self.tabBarController.tabBar.hidden = NO;
    //    self.navigationController.navigationBar.hidden= NO;
    //    [[UIApplication sharedApplication]setStatusBarHidden:NO];
    //    
        
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        [self CreatUI];
      
        //横屏同志UIApplicationDidChangeStatusBarFrameNotification   UIDeviceOrientationDidChangeNotification
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
    
    -(void)CreatUI{
        
        self.view.backgroundColor = [UIColor blackColor];
        
        webview = [[UIWebView alloc] initWithFrame:self.view.bounds];
        
        [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.cnblogs.com/xujiahui/p/6583204.html"]]]];
        webview.delegate = self;
        
        webview.scalesPageToFit = YES;
        // webview.scrollView.scrollEnabled = NO;
        [self.view addSubview:webview];
        
        //back。是一个button
        //back = [myButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.view.frame.size.width-80, self.view.frame.size.height-100, 40,40) tag:1 image:nil andBlock:^(myButton *button) {
        
        back= [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-80, self.view.frame.size.height-100, 40,40)];
        
        
        [self.navigationController popViewControllerAnimated:YES];
      
        back.backgroundColor = [UIColor greenColor];
        
        
        
        [self.view addSubview:back];
        
        
    }
    
    
    
    //横屏//横屏  状态下  width和height是颠倒的
    - (void)deviceOrientationDidChange
    {
        NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
        
        if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
            
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
            
            [UIView animateWithDuration:0.3f animations:^{
                
                self.view.transform = CGAffineTransformMakeRotation(0);
                self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
                
                webview.frame = self.view.bounds;
                
                back.frame = CGRectMake(SCREEN_WIDTH-80, SCREEN_HEIGHT-100, 60, 60);
                
                
            }];
            
            
            
            
            //注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight
        } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft ) {
            
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
            
            [UIView animateWithDuration:0.3f animations:^{
                
                self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
                
                self.view.bounds = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);
                
                webview.frame = self.view.bounds;
                
                back.frame = CGRectMake(SCREEN_WIDTH-80, SCREEN_HEIGHT-10, 60, 60);
                
                
            }];
            
            
            
            
        }else if ( [UIDevice currentDevice].orientation== UIDeviceOrientationLandscapeRight){
            
            
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
            
            [UIView animateWithDuration:0.3f animations:^{
                
                self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
                
                self.view.bounds = CGRectMake(0, 0,SCREEN_HEIGHT, SCREEN_WIDTH);
                
                webview.frame = self.view.bounds;
                
                back.frame = CGRectMake(SCREEN_WIDTH-80, SCREEN_HEIGHT-100, 60, 60);
                
                
            }];
        }
    }
    
    
    @end
  • 相关阅读:
    Python第三方库SnowNLP(Simplified Chinese Text Processing)快速入门与进阶
    Python第三方库SnowNLP(Simplified Chinese Text Processing)快速入门与进阶
    用python玩微信(聊天机器人,好友信息统计)
    BIOS与UEFI
    MBR&/BOOT&GRUB
    Hard Disk Driver(GPT)
    Hard Disk Drive(MBR)
    反Secure Boot垄断:兼谈如何在Windows 8电脑上安装Linux
    硬盘分区基本知识
    计算机启动过程
  • 原文地址:https://www.cnblogs.com/xujiahui/p/6583204.html
Copyright © 2011-2022 走看看