Manual rotation of IOS Screen

Source: Internet
Author: User
First, manually change the attributes of view. Transform.

Specific Method:

   View. transform is generally a view rotation, stretching, moving, and other attributes, similar to view. layer. transform, the difference lies in view. transform is two-dimensional, that is, the method of using affine is usually the class with the prefix cgaffinetransform (you can search for all classes in the prefix in the API documentation), and view. layer. transform can be changed in the 3D mode. Generally, classes prefixed with catransform3d are used.

   Remember that when you change a view. transform attribute or view. layer. if you need to restore the default status when using transform, remember to reset them to use view. transform = cgaffinetransformidentitY, or view. layer. transform = catransform3didentity. Suppose you keep changing a view. transform attributes. If the attributes are not reset before each change, you will find that the subsequent changes and the changes you want are not the results you really want.

   Okay, the rotation attribute is described above, and the next step is the key. The official solution is to check the status of the current battery. uiinterfaceorientation orientation = [uiapplication sharedapplication]. statusbarorientation; in this way, you can know the display direction of the battery bar on the current screen, and you can also set its display direction forcibly. By setting this attribute, you will be OK, you can choose whether to animated or not to change the direction of the battery bar. With these two, we can change the display method we want.

1. Obtain the direction of the current battery. uiinterfaceorientation orientation = [uiapplication sharedapplication]. statusbarorientation

2. Obtain the current screen size cgrect frame = [uiscreen mainscreen]. applicationframe;

3. Set the center of our view
   Cgpoint center = cgpointmake (frame. Origin. x + Ceil (frame. Size. width/2), frame. Origin. Y + Ceil (frame. Size. Height/2 ));

4. Obtain the angle of rotation based on the direction of the current battery. Usually

If (orientation = uiinterfaceorientationlaNdscapeleft ){
       Return cgaffinetransformmakerotAtion (m_pi * 1.5 );
   } Else if (orientation = uiinterfaceorientationlaNdscaperight ){
       Return cgaffinetransformmakerotAtion (m_pi/2 );
   } Else if (orientation = uiinterfaceorientationpoRtraitupsidedown ){
       Return cgaffinetransformmakerotAtion (-m_pi );
   } Else {
       Return cgaffinetransformidentitY;
   }

5. The display mode of our view can be changed through animation.
[[Uiapplication sharedapplication] setstatusbarorientation: uideviceorientationlandsCaperight animated: Yes];

Cgfloat duration = [uiapplication sharedapplication]. statusbarorientationanimAtionduration; (gets the animation Change Time of the current battery bar)
       [Uiview beginanimations: Nil context: Nil];
       [Uiview setanimationduration: Duration];

      // Set the Rotation Angle of View. Transform to match.

       [Uiview commitanimations];

Second: Use setorientation: to forcibly rotate to a specific direction.

    Note: Apple does not support this method after 3.0. This method has become private, but it requires a clever way to skip the app stroe review.

   Do not directly call [[uidevice currentdevice] setorientation: uiinterfaceorientationlaNdscaperight] This method is mandatory for landscape screens, which makes it difficult for your program to pass the App Store review. However, you can use the performselector method to call it. The specific code is as follows:

// Force horizontal screen
   If ([[uidevice currentdevice] respondstoselector: @ selector (setorientation :)]) {
       [[Uidevice currentdevice] performselector: @ selector (setorientation :)
                                      Withobject :( ID) uiinterfaceorientationlaNdscaperight];
   }

   Conclusion: if the first method can meet your needs, it is best to use the first method, because the App Store certainly does not ask questions, but the second method must be risky, however, if your structure is too complex and the first method is difficult to control, you can try the second method. In the sample provided by youmi, I saw that he used the second simple method to display the vertical advertisement bar.

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.