Two ways to set the screen orientation for iOS

Source: Internet
Author: User

The first is to change the properties of view.transform by artificial means.

Specific measures:

View.transform is generally the rotation of the view, stretching the movement and other properties, similar to View.layer.transform, the difference is that View.transform is two-dimensional, that is, the use of affine method is usually prefixed with the Cgaffinetransform class (can go to a The PI document searches for all classes of this prefix, and view.layer.transform can change under 3D mode, usually using a class prefixed with catransform3d.

One thing to remember here is that when you change a View.transform property or view.layer.transform You need to restore the default state, remember to reset them first to use View.transform = Cgaffinetransformidentity, or View.layer.transform =catransform3didentity, Assuming you're constantly changing a view.transform property, and you don't reset it before each change, you'll see that the changes you've made and what you want are changing, not the results you really want.

Well, the properties of rotation are described above, and the next is the key. An official offer is to check the current battery bar status uiinterfaceorientationorientation = [Uiapplicationsharedapplication].statusbarorientation; This way, you can know the current screen of the battery bar display direction, and you can also force the setting of his display direction, by setting this property is OK, you can choose whether to animate the direction of the battery bar. With these two, we can arbitrarily change the way we want to display.

1. Get the current battery bar direction uiinterfaceorientation orientation = [Uiapplicationsharedapplication].statusbarorientation

2. Get the current screen size cgrect frame = [Uiscreenmainscreen].applicationframe;

3. Set the center point of our view
Cgpointcenter = Cgpointmake (frame.origin.x + ceil (FRAME.SIZE.WIDTH/2), FRAME.ORIGIN.Y + ceil (FRAME.SIZE.HEIGHT/2));

4. Depending on the direction of the current battery bar, get the size of the angle you want to rotate. 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. We can animate the way our view is displayed .
[[UIApplication Sharedapplication]setstatusbarorientation:uideviceorientationlandscaperightanimated:yes];

cgfloat duration = [Uiapplicationsharedapplication].statusbarorientationanimationduration; (Gets the time of the current battery bar animation change)
[UIView Beginanimations:nil context:nil];
[UIView setanimationduration:duration];

//Set the View.transform to match the size of the rotation angle here.

[UIView commitanimations];

The second type: by means of SetOrientation: The method is forced to rotate into a particular direction.

Note: Apple does not support this option after 3.0, and this approach has become private, but to skip the Appstroe audit requires a bit of ingenious approach.

Do not directly invoke [[Uidevice Currentdevice] setorientation:uiinterfaceorientationlandscaperight] Such a way to enforce the horizontal screen, This makes your program difficult to audit by AppStore. But you can choose to use the Performselector method to invoke it. Just a few lines of code are as follows:

//Force horizontal screen
    if ([[Uidevice Currentdevice]respondstoselector: @selector (setorientation:)]) {
       [[ Uidevice currentdevice]performselector: @selector (setorientation:)
                                        withobject: (ID) uiinterfaceorientationlandscaperight];
    }

Summary: If the first way to meet your needs, it is best to use the first approach, because the AppStore certainly did not ask the question, but the second is to take the risk, but if your structure is too complex, leading to the use of the first method is difficult for people to control, you can try to use a simple second method. I saw him in a sample of the rice supply. The second simple way to display a horizontal screen vertical banner

Two ways to set the screen orientation for iOS

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.