zoukankan      html  css  js  c++  java
  • OC屏幕旋转相关

    OC屏幕旋转分为两个部分来说,第一个是开启了Device Orientation,开启了的话,自己旋转,没开启需要自己手动处理。因为现在大多数都是用自动布局,这个一般用不到,最近在看AVFoundation相关的东西,需要用到这个,所以总结下

     第一部分,开启了自动旋转:

    • (1)注册屏幕旋转通知:
     [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        //注册屏幕旋转通知
     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(orientChange:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:[UIDevice currentDevice]];
    
    - (void)orientChange:(NSNotification *)notification{
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if(orientation == UIDeviceOrientationPortrait){
             NSLog(@"home在下方");
            self.deviceOrientationBtn.selected = NO;
        }else if(orientation == UIDeviceOrientationPortraitUpsideDown){
             NSLog(@"home在上方");
            self.deviceOrientationBtn.selected = NO;
        }else if(orientation == UIDeviceOrientationLandscapeLeft){
             NSLog(@"home在右方");
            self.deviceOrientationBtn.selected = YES;
        }else if(orientation == UIDeviceOrientationLandscapeRight){
             NSLog(@"home在左方");
            self.deviceOrientationBtn.selected = YES;
        }
    
    }
    

     如果开启了之后我想手动横屏或者竖屏的话,也可以使用代码强制实现:

    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight]
    

     or

     [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"]
    

     如果想进入改ViewController就横屏或者竖屏,退出之后恢复的话,可以在

    -(void)viewWillAppear:(BOOL)animated
    

     and

    -(void)viewWillDisappear:(BOOL)animated:
    

     两个方法中实现通知注册了之后dealloc别忘记内存处理

    -(void)dealloc{
        [[NSNotificationCenter defaultCenter]removeObserver:self];
    }
    

    第二部分,没有开启屏幕旋转,需要自己手动做一个假的来代替

    (1)横屏设置的方法:

    - (void)fullScreenWithDirection:(UIInterfaceOrientation)direction{
        if (direction == UIInterfaceOrientationLandscapeLeft){
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
            [UIView animateWithDuration:0.25 animations:^{
                self.view.transform = CGAffineTransformMakeRotation(M_PI / 2);
            }completion:^(BOOL finished) {
    //            [self setFullStatusBarHiddenType:_fullStatusBarHiddenType];
            }];
        }else if (direction == UIInterfaceOrientationLandscapeRight) {
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
            [UIView animateWithDuration:0.25 animations:^{
                self.view.transform = CGAffineTransformMakeRotation(0);
            }completion:^(BOOL finished) {
    //            [self setFullStatusBarHiddenType:_fullStatusBarHiddenType];
            }];
        }
        self.view.frame = [[UIApplication sharedApplication]keyWindow].bounds;
        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
        
    }
    

    (2)竖屏还原

    [UIView animateWithDuration:0.25 animations:^{
                self.view.transform = CGAffineTransformMakeRotation(0);
                self.view.frame = [[UIApplication sharedApplication]keyWindow].bounds;
                [self.view setNeedsLayout];
                [self.view layoutIfNeeded];
            }completion:^(BOOL finished) {
            }];
    
  • 相关阅读:
    mybatis 乐观锁和逻辑删除
    JAVA实现DES加密实现详解
    axios 全攻略之基本介绍与使用(GET 与 POST)
    Ajax json 数据格式
    CentOS 7安装Hadoop 3.0.0
    <p>1、查询端口号占用,根据端口查看进程信息</p>
    CentOS查询端口占用和清除端口占用的程序
    Spring Boot Maven 打包可执行Jar文件!
    linux下运行jar
    maven 工程mybatis自动生成实体类
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/11548111.html
Copyright © 2011-2022 走看看