IOS: Study Notes on screen Rotation

Source: Internet
Author: User

Study Notes on screen Rotation

 

The accelerator is the basis for the entire IOS screen rotation. Only by relying on the accelerator can the device determine the current device direction. The IOS system defines the following seven device directions:

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

I. uikit process screen Rotation

When the accelerator detects a direction change, it will send a uideviceorientationdidchangenotification notification. In this way, any view that cares about the direction change can register this notification and respond accordingly when the device direction changes. In the previous blog, we have mentioned that when the screen is rotated, uikit has helped us to do a lot of things so that we can complete screen rotation.

The following figure shows how to rotate the uikit screen:

1. When the device rotates, uikit receives a rotation event.

2. uikit notifies the current user through appdelegateProgram.

3. Window will know its rootviewcontroller and determine the supported rotation direction of the View Controller to complete the rotation.

4. If a pop-up View Controller exists, the system determines whether to rotate it based on the pop-up view controller.

 

Ii. uiviewcontroller achieves screen Rotation

In response to device rotation, we can use the uiviewcontroller method to achieve more fine-grained control. When View Controller receives a change in the direction from the window, the process is as follows:

1. First, determine whether the current viewcontroller supports rotation to the target direction. If yes, go to process 2. Otherwise, the rotation process ends directly.

2. CallWillrotatetointerfaceorientation: Duration:Method to notify the View Controller to rotate to the target.If the viewcontroller is a container ViewController, it will continue to call the method of its content View Controller. At this time, we can also temporarily hide some views and wait until the rotation ends before the reality comes out.

3. Adjust the View Controller's Bounds displayed in the window. A change in the View Controller's bounds will triggerViewwilllayoutsubviews method. This timeSelf. interfaceorientationAndThe statusbarorientation direction is still the original direction.

4. Next, the current view controller'sWillanimaterotationtointerfaceorientation: Duration:The method is called. The system will change all the attributes executed in this method.Put it in the animation block.

5. Execute an animation in the direction of rotation.

6. Final call Didrotatefrominterfaceorientation:Method to notify the View Controller to complete the rotation animation. In this case, we can display the hidden view in the second part.

Shows the entire response process:

The above is the complete screen rotation process of uikit. We only need to follow the prompts to make corresponding processing to perfectly support screen rotation.

 

Iii. Precautions and Suggestions

1) Considerations

When our view controller is hidden, the device direction may also change.For example, when View Controller A pops up a full screen View Controller B, A cannot receive the screen rotation message because a is completely invisible. In this case, if the screen direction changes and dismiss B, the direction of A is incorrect. We can fix this problem by updating the direction in View Controller A's viewwillappear.

2) some suggestions for screen Rotation

    • Response to temporary interface operations during rotation.
    • Before and after rotation, try to keep the current position unchanged.
    • When the view level is complex, to improve the efficiency, replace the current view level before the rotation starts, and then replace the original view level after the rotation ends.
    • It is best to force reload tableview after rotation to ensure that the new row can be full screen after the direction changes. For example, for some photo display interfaces, only one column is displayed on the vertical screen, but the list interface is displayed on the horizontal screen. At this time, more elements are displayed on the interface, in this case, reload content is necessary.

 

Note: The above content and illustrations are all from Apple's official documentation: View Controller Programming Guide for iOS

Related Article

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.