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

来源:互联网 发布:解不定积分软件 编辑:程序博客网 时间:2024/06/09 14:32

有的时候,在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];

}




0 0
原创粉丝点击