iOS screen rotation

Source: Internet
Author: User
Tags uikit

Accelerometers are the basis for the entire iOS screen rotation, depending on the accelerometer. The device is capable of inferring the current device orientation, and 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 for example the following four interface orientations:

typedef ns_enum (Nsinteger, uiinterfaceorientation) {
uiinterfaceorientationportrait = uideviceorientationportrait,
Uiinterfaceorientationportraitupsidedown = Uideviceorientationportraitupsidedown,
Uiinterfaceorientationlandscapeleft = Uideviceorientationlandscaperight,
Uiinterfaceorientationlandscaperight = Uideviceorientationlandscapeleft
};

Uikit process for processing screen rotation

When the accelerometer detects a change in direction, a uideviceorientationdidchangenotification notification is issued so that any view that is concerned about the direction change can respond to changes in the direction of the device by using the notification.

In the previous blog, we have already mentioned the time when the screen rotates. Uikit helped us do a lot of things. It is convenient for us to finish the screen rotation.



The process of uikit the corresponding screen rotation is as follows:

1, when the device rotates, uikit receive the rotation event.



2, Uikit through Appdelegate notify the current program window.

3, window will know its rootviewcontroller, infer that the view controller supports the direction of rotation, complete rotation.



4, if there is a pop-up view controller, the system will be based on the pop-up view controller. To infer whether or not you want to rotate.

 

Second, uiviewcontroller implement screen rotation

When responding to device rotation. We are able to achieve finer-grained control by Uiviewcontroller, when the view controller receives a change in the direction the window is coming from. The process, such as the following:

1, first infer whether the current Viewcontroller support rotation to the target direction, assuming the support to enter process 2, otherwise the rotation process directly ended.

2, call Willrotatetointerfaceorientation:duration: method. Notifies the view controller that it will be rotated to the target direction. Suppose the Viewcontroller is a container view controller. It will continue to invoke the method of its content view controller. This time we can also temporarily hide some of the view. After the rotation is over, come out in reality.

3, window adjusts the bounds of the displayed view controller, because the bounds of the view controller changes, the Viewwilllayoutsubviews method is triggered. This time self.interfaceorientation and statusbarorientation direction is still the same direction.

4, followed by the current View controller's Willanimaterotationtointerfaceorientation:duration: method will be called. The system will put all the attributes that are running in the method into the animation block.



5. Rotate the animation in the direction of operation.

6, the Last Call Didrotatefrominterfaceorientation: method, notify the view controller rotation animation run complete. This time we can show the second hidden view again.

The entire response step is for example as seen in:



  

The above is the Uikit next full screen rotation process. We only need to follow the instructions to make the corresponding processing can be perfect to support screen rotation.





Iii. Considerations and Recommendations

1) Precautions

When our view controller is hidden. The direction of the device may also vary.

For example, when View Controller a pops up a full-screen view Controller B, because a is completely invisible, the screen rotation message is not received.

This time, assuming that the screen orientation changes, and then dismiss B, the direction of a will be wrong.

We can fix this problem by updating the direction in the Viewwillappear of view controller A.

2) Some suggestions when the screen rotates

In the process of rotation. The response of the temporary interface operation.


Before and after rotation, try to display the current position unchanged.


For more complex view levels, to improve efficiency, replace the current view level before rotation, and then replace the original view level with the rotation.
It is best to force reload TableView after rotation to ensure that the new row can be filled with full screen after the direction changes. For example, for some photo-display screens, the vertical screen displays only one column. However, a horizontal screen displays the list interface. At this point, an interface will show many other elements. At this point the reload content is very necessary.

iOS screen rotation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.