您的位置:首页 > 移动开发 > IOS开发

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

2015-06-04 09:57 513 查看
有的时候,在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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: