iOS手动旋转屏幕、自动旋转屏幕

有的时候,在iOS下需要实现手动旋转屏幕与自动旋转屏幕的自由切换。比如,在手机竖屏时希望点击按钮使页面横屏,然后当手机屏幕方向发生变化时,页面能正常的契合手机旋转的方向。

1、视图方向旋转:通过view.transform实现view的缩放、旋转、平移等操作。在页面旋转时,需要关闭自动旋转:

- (BOOL)shouldAutorotate

{

    return NO;

}

这样在屏幕旋转时才不会使画面方向混乱。具体操作根据transform属性实现即可,这种方法无法实现手动旋转与自动旋转的自由切换。


2、设备方向旋转分两种:一种是根据设备重力加速度自动实现旋转,另一种是通过代码调用UIDevice的setOrientation实现旋转。注:这种方式上传App Store有风险。

开启ARC情况下:

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

        SEL selector = NSSelectorFromString(@"setOrientation:");

        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];

        [invocation setSelector:selector];

        [invocation setTarget:[UIDevice currentDevice]];

        int val = UIInterfaceOrientationLandscapeRight;

        [invocation setArgument:&val atIndex:2];

        [invocation invoke];

}

未开启ARC情况下:

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationLandscapeRight];

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值