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];
}
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];
}
相关文章推荐
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记28 ScrollView 滑动视图
- IOS开发之AVAudioPlayer
- iOS的hook方案: Method Swizzling
- ios修改导航条title的颜色
- facebook 开源的一个 移动开发框架
- JS对于Android和IOS平台的点击响应的适配
- 构建 iOS 风格移动 Web 应用程序的8款开发框架
- ios开发学习笔记040-autolayout 第三方框架Masonry
- IOS开发中的几种设计模式介绍
- iOS应用开发:什么是ARC?
- 1000 个 iOS 常用的库
- [转]IOS开发中四种解析Json的方法
- xcode6.3 模版位置
- IOS8中Corelocation调用修改
- BIOS基本知识
- iOS图片的截取(OC)
- iOS开发—iOS多线程编程之NSThread的使用
- iOS日期与时间处理
- iOS打电话,发短信,发邮件,打开网址
- iOS开发学习第十二课——函数指针定义 、函数回调 、动态排序