Ios 屏幕旋转

研究了下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

};



UIInterfaceOrientation是软件的方向,可以读取可以设置。

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];



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值