您的位置:首页 > 其它

关于通过屏幕旋转确定view的layout的问题

2017-03-22 12:52 316 查看
之前在项目中,有一个UIImageView需要通过接收屏幕旋转的通知来计算它的大小及位置,开始我选择接收

UIDeviceOrientationDidChange通知来处理,后来发现个别情况计算出来的大小位置不对,经过排查发现我的代码里只处理是水平还是垂直的逻辑,然而device的orientation却还包含了face up和face down的情况,并且即便你的app并不支持这俩个orientation,但是由于orientation确实改变了,所以还是会接到。但我确实不需要这俩个orientation,经过查找资料,得到如下答复“

Apple recommends against using device orientation for view layout. Instead, each view controller has an interfaceOrientation property,
and 
UIApplication
 has
statusBarOrientation property,
both of which will return the current interface orientation, which is suitable for view layout.

To monitor for changes, there are 
UIViewController
 methods
like willRotateToInterfaceOrientation:duration: that
will be called and notifications such as UIApplicationWillChangeStatusBarOrientationNotification that
will be posted when an interface orientation change occurs.
”,大致意思是说,苹果并不支持你去使用device的orientation改变这个通知,去确定你的view的layout,而每个viewcontroller有其自己的interface orientation属性,同时整个app的uiapplication有一个statusbar orientation的属性,在答复的最下面,也给出了,你可以通过接收 UIApplicationWillChangeStatusBarOrientationNotification来获取屏幕旋转的通知,而这个通知最棒的地方也就在于,它只处理垂直和水平俩种情况,很显然,因为statusbar位置的改变也就只关心垂直还是水平!

至此,这个关于屏幕旋转的通知基本上就清楚了,如果你只关心水平和垂直之间的改变,那么请选择接收 UIApplicationWillChangeStatusBarOrientationNotification,因为接收UIDeviceOrientationDidChange所带来的face
up和face down可能会给你的逻辑处理带来意想不到的BUG,如果你需要face up和face down的改变,比如有些需求需要face down的时候屏幕变暗等,那么你需要接收它来详细的区分各种orientation~

总之,慎重使用UIDeviceOrientationDidChange来确定view的layout(其实最好不要用,官方不推荐,本身也有更好的)~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: