码迷,mamicode.com
首页 > 其他好文 > 详细

OC屏幕旋转相关

时间:2019-09-19 12:26:26      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:value   alt   ram   set   hid   isa   ati   status   add   

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:

 两个方法中实现

 

 

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

(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) {
        }];

 

OC屏幕旋转相关

标签:value   alt   ram   set   hid   isa   ati   status   add   

原文地址:https://www.cnblogs.com/hualuoshuijia/p/11548111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!