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

 

转载于:https://www.cnblogs.com/hualuoshuijia/p/11548111.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值