Objective-C屏幕旋转

捕获屏幕旋转
1.注册系统通知:(适用于所有页面)

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

此方法监听的是状态栏的变化,所以系统自动旋转被锁定的时候无法监听的到,常用此方法与系统设置保持一致,还可以监听UIDeviceOrientationDidChangeNotification这个通知,当系统自动旋转呗锁定时依然可以执行,当我们关注的只是物理朝向时,我们通常需要注册该通知来解决问题。

屏幕的旋转朝向可以通过 [[UIDevice currentDevice]orientation] 判断:

- (void)changeRotate:(NSNotification*)notify{

        if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
            || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
            //竖屏

        } else {
            //横屏

        }

}

2.willAnimateRotationToInterfaceOrientation:适用于UIViewController,在你的UIViewController中重写该方法:

-( void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        //竖屏
    }
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        //横屏
    }

}

手动调用系统旋转屏幕方法:

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

上面是旋转到竖屏,可以用UIDeviceOrientationLandscapeLeft或者UIDeviceOrientationLandscapeRight旋转到横屏。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值