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) {
            }];
    
  • 相关阅读:
    简单理解ThreadLocal原理和适用场景
    Portal实现原理
    Spring cloud微服务实战——基于OAUTH2.0统一认证授权的微服务基础架构
    Java8中 Date和LocalDateTime的相互转换
    sonar rule
    图论篇2——最小生成树算法(kurskal算法&prim算法)
    图论篇1——图的基本概念
    数论篇6——欧拉函数
    数论篇5——数论四大定理
    数论篇4——逆元(数论倒数)
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/11548111.html
Copyright © 2011-2022 走看看