Screen rotation study notes

Screen rotation study notes

 

http://www.cnblogs.com/smileEvday/archive/2013/04/24/Rotate2.html

 

  The accelerometer is the basis of the entire IOS screen rotation, and the device can determine the current device orientation only by relying on the accelerometer. The IOS system defines the following seven device orientations:

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

   And the following four interface directions:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait =  UIDeviceOrientationPortrait ,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};
 

1. The process of UIKit handling screen rotation 

  When the accelerometer detects an orientation change, it will issue a  UIDeviceOrientationDidChangeNotification  notification, so that any view that cares about the orientation change can register for the notification and respond accordingly when the orientation of the device changes. In the previous blog, we have mentioned that when the screen is rotated, UIKit helps us do a lot of things to facilitate us to complete the screen rotation.

  The flow of UIKit's corresponding screen rotation is as follows:

1. When the device is rotated, UIKit receives a rotation event.

2. UIKit notifies the window of the current program through AppDelegate.

3. Window will notify its rootViewController, determine the rotation direction supported by the view controller, and complete the rotation.

4. If there is a pop-up view controller, the system will judge whether to rotate according to the pop-up view controller.

 

Second, UIViewController realizes screen rotation

  When responding to the rotation of the device, we can achieve more fine-grained control through the methods of UIViewController. When the view controller receives the direction change from the window, the process is as follows:

1. First, determine whether the current viewController supports rotating to the target direction, and if so, enter process 2, otherwise the rotation process ends directly.

2. Call  willRotateToInterfaceOrientation:duration: the method to notify the view controller that it is about to rotate to the target direction. If the viewController is a container view  controller, it will continue to call this method on its content view controller. At this time, we can also temporarily hide some views and show them in reality after the rotation is over.

3. When the window adjusts the bounds of the displayed view controller, since the bounds of the view controller changes,  the direction of viewWillLayoutSubviews 方法。这个时候self.interfaceOrientation and statusBarOrientation will still be triggered.

4. Then the  willAnimateRotationToInterfaceOrientation:duration: method of the current view controller will be called. The system will put all property changes performed in this method into the animation block.

5. Execute the animation of the direction rotation.

6、最后调用 didRotateFromInterfaceOrientation: 方法,通知view controller旋转动画执行完毕。这个时候我们可以将第二部隐藏的view再显示出来。(这里重新修改View的位置即可)

  整个响应过程如下图所示:

  

  以上就是UIKit下一个完整的屏幕旋转流程,我们只需要按照提示做出相应的处理就可以完美的支持屏幕旋转。

 

三、注意事项和建议

  1)注意事项

  当我们的view controller隐藏的时候,设备方向也可能发生变化。例如view Controller A弹出一个全屏的view controller B的时候,由于A完全不可见,所以就接收不到屏幕旋转消息。这个时候如果屏幕方向发生变化,再dismiss B的时候,A的方向就会不正确。我们可以通过在view controller A的viewWillAppear中更新方向来修正这个问题。

  2)屏幕旋转时的一些建议

  • 在旋转过程中,暂时界面操作的响应。
  • 旋转前后,尽量当前显示的位置不变。
  • 对于view层级比较复杂的时候,为了提高效率在旋转开始前使用截图替换当前的view层级,旋转结束后再将原view层级替换回来。
  • 在旋转后最好强制reload tableview,保证在方向变化以后,新的row能够充满全屏。例如对于有些照片展示界面,竖屏只显示一列,但是横屏的时候显示列表界面,这个时候一个界面就会显示更多的元素,此时reload内容就是很有必要的。

 

  注:以上内容和插图全部来自苹果官方文档:View Controller Programming Guide for iOS

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326833470&siteId=291194637