研究了下ios屏幕旋转,从网上找了很多文章 http://blog.csdn.net/zzfsuiye/article/details/8251060 这篇说的非常详细,和大家分享下。
下面是真机调试时遇到的一些问题,都很小白,记下来做笔记。
1、按照示例写好了demo发现转动手机屏幕,程序都没做任何反应,反反复复看了代码也没有找到问题,最后,发现是手机之前设置成了锁屏状态。。。
取消了锁屏,一切正常。
2、UIViewController里面实现了控制旋转的函数,但是当viewController被UINavigationController或UITabBarController使用时,控制函数失效,想要控制旋转
需要自定义UINavigationController或UITabBarController的类别,在类别里区实现控制旋转的函数,上边的文章写得很详细。
3、这些控制都是在UIViewController里里面实现的,那有没有直接获取手机旋转的事件呢?查了一下找到了UIDeviceOrientationDidChangeNotificatio;
UIDeviceOrientation是设备的方向,只能读取不能设置,支持6个方向,
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down
};
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
注意:UIInterfaceOrientation的横屏的左边和右边跟UIDeviceOrientation刚好相反。
如果需要获取设备方向变化(UIDeviceOrientation)的消息的话,需要注册UIDeviceOrientationDidChangeNotification通知。
在注册通知时,需要先调用UIDevice的beginGeneratingDeviceOrientationNotifications方法
同时,在结束时,需要移除改通知消息[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[notificationCenter addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
[notificationCenter removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];