How to write control screen rotation in IOS development summary _ios

Source: Internet
Author: User
Tags versions

In iOS5.1 and previous versions, we used shouldautorotatetointerfaceorientation: to individually control the direction of a Uiviewcontroller's spin-screen support, such as:

Copy Code code as follows:

-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
return (interfaceorientation = = uiinterfaceorientationportrait);
}

In iOS6, however, this method is discarded and is not used.
Shouldautorotatetointerfaceorientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in IOS 6.0.) Override the Supportedinterfaceorientations andpreferredinterfaceorientationforpresentation methods instead.)

In practice, it will be found that the screen cannot be locked by supportedinterfaceorientations alone control.

Copy Code code as follows:

-(Nsuinteger) supportedinterfaceorientations
{
return uiinterfaceorientationmaskportrait;
}

After several experiments to summarize the control of screen rotation support direction of the following methods:
Sub-class Uinavigationcontroller, adding methods

Copy Code code as follows:

-(BOOL) shouldautorotate
{
return self.topViewController.shouldAutorotate;
}

-(Nsuinteger) supportedinterfaceorientations
{
return self.topViewController.supportedInterfaceOrientations;
}

and set it as a program entry, or designate it as a Self.window.rootViewController
Then add your own view controller, if you want to disable the spin screen of a View controller: (Support for all versions of control)
Copy Code code as follows:

-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
return (interfaceorientation = = uiinterfaceorientationportrait);
}

-(BOOL) shouldautorotate
{
return NO;
}

-(Nsuinteger) supportedinterfaceorientations
{
return uiinterfaceorientationmaskportrait;
}

If you want to turn on the full direction of a view controller, turn the screen support:

Copy Code code as follows:

-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return (interfaceorientation!= uiinterfaceorientationportraitupsidedown);
}

-(Nsuinteger) supportedinterfaceorientations
{
return uiinterfaceorientationmaskallbutupsidedown;
}

-(BOOL) shouldautorotate
{
return YES;
}

Thus, the individual control of each view controller is realized.

Incidentally, if all the view controller for the entire application does not support the spin screen, then simply:

Copy Code code as follows:

-(Nsuinteger) Application: (UIApplication *) application Supportedinterfaceorientationsforwindow: (UIWindow *) window
{
return uiinterfaceorientationmaskportrait;
}

What to do when the view is messed up?
First, we must look at the following 4 states, which are used to describe the direction of the device rotation:

For the processing of the rotating screen, roughly divided into the following several situations and ideas:
Maybe, you don't need to rotate the screen support, but want to lock

Copy Code code as follows:

-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
return NO;
}

Perhaps, you need to support the spin screen, or to support a part of the direction of the rotation screen
Copy Code code as follows:

-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation {
Return (interfaceorientation!= uiinterfaceorientationportraitupsidedown);
}

Perhaps, your view has a background picture, the system helps you to stretch the picture when you rotate the screen, but it doesn't control your other parts, such as button, you want to change the button's size and position directly.

Copy Code code as follows:

-(void) Willanimaterotationtointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: ( Nstimeinterval) duration
{
if (uiinterfaceorientationisportrait (tointerfaceorientation)) {
NSLog (@ "Now is the vertical screen");
[Btn Setframe:cgrectmake (213, 442, 340, 46)];
}
if (Uiinterfaceorientationislandscape (tointerfaceorientation)) {
NSLog (@ "Now is horizontal screen");
[Btn Setframe:cgrectmake (280, 322, 460, 35)];
}
}

Maybe you don't want to constrain the control with absolute coordinates, but you want it to rotate itself to fit the screen.
Copy Code code as follows:

-(void) viewdidload
{
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.
Uidevice *device = [Uidevice currentdevice];
[Device begingeneratingdeviceorientationnotifications];
Using Nsnotificationcenter to obtain rotary signal uideviceorientationdidchangenotification
Nsnotificationcenter *ncenter = [Nsnotificationcenter defaultcenter];
[Ncenter addobserver:self selector: @selector (orientationchanged) name:uideviceorientationdidchangenotification Object:device];
}

-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return (interfaceorientation!= uiinterfaceorientationportraitupsidedown);
}

-(void) Rotation_btn: (float) n
{
UIButton *robtn = self.btn;
Robtn.transform = Cgaffinetransformmakerotation (n*m_pi/180.0);
}

-(void) orientationchanged
{
Uideviceorientation Orientaiton = [[Uidevice currentdevice] orientation];

Switch (Orientaiton) {
Caseuideviceorientationportrait:
[Self rotation_btn:0.0];
Break
Caseuideviceorientationportraitupsidedown:
[Self rotation_btn:90.0*2];
Break
Caseuideviceorientationlandscapeleft:
[Self rotation_btn:90.0*3];
Break
Caseuideviceorientationlandscaperight:
[Self rotation_btn:90.0];
Break
Default
Break
}
}

Maybe, you need autoresizessubviews = YES
Perhaps, you want the screen to have different layout effect, need to prepare 2 copies of Subview, in different states to replace
Of course, do not forget that you need to adjust the set diagram in 1, 2,

To help us achieve the adaptations we want. The Example animation appears very clear, ^_^ I will not be long-winded.

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.