iOS manual Control Interface rotation

Source: Internet
Author: User

All roads lead to Rome, and the means to solve the same problem are varied. For the cases mentioned in "controlling individual view rotation cases with IOS 6 and above ", we use the system's own rotation mechanism to solve the problem. Similarly, we can solve the problem ourselves coding, and the final effect is exactly the same as the rotation animation effect of the system. Needless to say, the following is about to explain.


The core idea of manual control of the interface rotation is to rotate the root view of the app using the transform attribute of the UIView. What is the root view? If your app's Window.rootviewcontroller is Uinavigationcontroller, then the root view is Navigationcontroller.view. For the effect of rotation and the consistency of the system, we also need to add a uiview animation to it.


then we will do a concrete operation, first of all, to establish a test project, engineering structure such as. The root view controller of the test project is a uinavigationcontroller, In this uinavigationcontroller stack there is view controller A (an instance of Firstviewcontroller) and Controller B (an instance of Secondviewcontroller), Controller B is entered by clicking the button push in controller A.

Add code to the SecondViewController.h file as follows

#import <UIKit/UIKit.h> @interface secondviewcontroller:uiviewcontroller@property (nonatomic,assign) BOOL islandscape;-(ibaction) rotatebtnclicked: (ID) sender; @end
Add code to the SECONDVIEWCONTROLLER.M file as follows

#import "SecondViewController.h" @implementation Secondviewcontroller#pragma mark-view Life cycle-(void) viewdidload{    [Super Viewdidload];    Self.title = @ "Second";    Self.islandscape = NO; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (chan                                               Geframes:) name:uideviceorientationdidchangenotification Object:nil];}    -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} #pragma mark-methods-(void) rotatetolandscapeleft{nstimeinterval duration = [[UIApplication sharedapplication] Statu    Sbarorientationanimationduration]; [UIView animatewithduration:duration animations:^{[[uiapplication sharedapplication] Setstatusbarorientation:uiint        Erfaceorientationlandscapeleft]; Self.navigationController.view.transform = CgaffinetransformmakerotatiOn (-M_PI/2);    Self.navigationController.view.bounds = CGRectMake (0, 0, [self screenheight], 320);    } completion:^ (BOOL finished) {self.islandscape = YES; }];} -(void) rotatetolandscaperight{nstimeinterval duration = [[UIApplication sharedapplication]    Statusbarorientationanimationduration]; [UIView animatewithduration:duration animations:^{[[uiapplication sharedapplication] Setstatusbarorientation:uiint        Erfaceorientationlandscaperight];        Self.navigationController.view.transform = Cgaffinetransformmakerotation (M_PI/2);    Self.navigationController.view.bounds = CGRectMake (0, 0, [self screenheight], 320);    } completion:^ (BOOL finished) {self.islandscape = YES; }];} -(void) rotatetoportrait{nstimeinterval duration = [[UIApplication sharedapplication]    Statusbarorientationanimationduration]; [UIView animatewithduration:duration animations:^{[[uiapplication sharedapplication] Setstatusbarorientation:uiint ErfaceorientationpoRtrait];        Self.navigationController.view.transform = cgaffinetransformmakerotation (0);    Self.navigationController.view.bounds = CGRectMake (0, 0, +, [self screenheight]);    } completion:^ (BOOL finished) {self.islandscape = NO; }];}    -(CGFloat) screenheight{if (iPhone5) {return 568.0;    } else {return 480.0; }} #pragma mark-actions-(ibaction) rotatebtnclicked: (ID) sender{if (!self.islandscape) {[Self rotatetolandscap    Eright];    } else {[self rotatetoportrait]; }} #pragma mark-handle notification-(void) Changeframes: (nsnotification *) notification{uideviceorientation DeviceOri    Entation = [[Uidevice currentdevice] orientation];    if (deviceorientation = = uideviceorientationlandscaperight) {[Self rotatetolandscapeleft];    } else if (deviceorientation = = Uideviceorientationlandscapeleft) {[Self rotatetolandscaperight];       } else if (deviceorientation = = uideviceorientationportrait) { [Self rotatetoportrait]; }} @end
among them, rotatetolandscapeleft/rotatetolandscaperight/rotatetoportrait is the core method of view rotation. We register an event notification in the Viewdidload method, and when the physical rotation direction of the device changes, we call the method Changeframes: To rotate the view accordingly, or to rotate the interface by clicking on a button. As you can see, there is also a uinavigationcontroller category in the test project. Category to add

#import "Uinavigationcontroller+ext.h" @implementation Uinavigationcontroller (EXT)-(BOOL) shouldautorotate{    return NO;} @end

Why would you like to add the Uinavigationcontroller category? Because we need to rotate the StatusBar (status bar) at the same time, to rotate the statusbar must return no in the Shouldautorotate method.


running the test project and rotating the device, you can see that we have successfully manually controlled the interface rotation, and the rotation animation effect is the same as the system's default rotation effect. Compared to the solution mentioned in the previous article, there is a higher degree of freedom, and developers are able to control the rotation of the interface in an effortless manner.

Test project: Download



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.