Automatic IOS Rotation

Source: Internet
Author: User

Many applications in the iphone can adapt the orientation of the screen according to the orientation of your mobile phone. Of course, not all applications support this function. Specifically, it depends on whether application developers have developed the corresponding functions, required or not
This function depends on the actual situation and requirements, but it is not difficult to achieve this effect. This article will start with a beginner and write my summary of this learning.
When iPhone 4 and 4s are vertically placed, the view height is 480 # px and the width is 320 # px. If the status bar is added, the height is 460px. When horizontal playback is performed, the height is 320px, the width is 480px, and the height of the status bar is 300px.
Three implementation methods: 1. Automatic Adjustment of all objects,
2. manually adjust the Object Location
3. Develop view versions in different directions for applications.
Either method needs to overwrite the method of UIViewController.
When the phone direction is changed, the system will call shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientataion interfacetion) to check whether the application supports
If the corresponding direction is changed, true is returned. By default, only one direction is supported, that is, automatic rotation is disabled. To enable the function, return true in the desired direction.

My current learning environment is mac10.8 and xcode4.5. At this time, IOS is 6.0. The shouldAutorotateToInterfaceOrientation method has been discarded in IOS6.0, so there is no overwrite method available.
So how does it enable automatic rotation of applications in the corresponding direction? I think it is here.

Rotation in four directions is supported by default. If you only want to support a certain direction or certain directions, you can select the corresponding direction, the settings can automatically rotate in four directions, but do not support upsidedown direction. To support the settings, you have to do it in the program. The implementation method is as follows:
Only portait is supported and cannot be rotated:

-(BOOL) shouldAutorotate
{
Return NO;
}

Rotation supported:
-(BOOL) shouldAutorotate
{
Return YES;
}
-(NSUInteger) supportedInterfaceOrientations {

Return UIInterfaceOrientationMaskLandscape; // UIInterfaceOrientationMaskLandscape, UIInterfaceOrientationMaskAll, UIInterfaceOrientationMaskAllButUpsideDown
}

Considering compatibility with earlier versions, it is best to add:
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return YES;
}

Simple Description:
UIInterfaceOrientationMaskLandscape supports left and right landscape screens

UIInterfaceOrientationMaskAll supports four orientation Rotation

UIInterfaceOrientationMaskAllButUpsideDown supports rotation except UpsideDown


1. automatically adjust attributes to adapt to automatic rotation
You do not need to write code to implement this method. You only need to set it in interface builder,

Speaking of interface builder, I don't know what IOS6.0 was like before. There is a Storyboards in IOS6.0, which is the same as the upgrade of interface builder and is better than it,
At the same time, the new project under IOS6 is automatically reclaimed by default, so if you don't want to use S # toryboards and automatic garbage collection, You have to remove the check box here when creating the project, for example.

In xcode4.5, automatic rotation is already performed by default. To adjust the orientation of the widget during rotation, select the control, for example, to locate the Size Inspector view.

In xcode4.5, you only need to remove the Use Autolayout in File Inspector, as shown in
The Size Inspector view is removed as follows.
When the mobile phone direction is changed, the root change of the object is in Auotsizing. On the left is the change of the object location and size when the object direction is changed, on the right side, you can see the effect after the settings. The effect is the effect when the mobile phone is horizontal.

The I on the left side indicates the position of the object, which remains unchanged during implementation and is variable when the dotted line is used.
The key header in it indicates the object size, which changes when implemented and remains unchanged when the dotted line is used. That is to say, if the object size can be changed, the object size will be automatically adjusted to meet the display requirements.

In many cases, this automatic adjustment attribute cannot meet the requirements. It can only change the location of the object automatically, and it can change the size of the hour to adapt to display, but it may not be the desired display effect. At this time, it may be more flexible to specify the object location by writing a program mobile phone.

2. manually adjust the Object Location
Manual means to adjust the object location, size, and so on through code implementation. In xib, you must connect the object to the variable in the program.
2.1 directly drag the control to the variables of the class through interface builder. For example.

After it is specified, for example, if you want to change the object layout during rotation, you need to call the corresponding method. Of course, some functions in IOS will be triggered when the mobile phone is rotated. Here we use this willAnimateSecondHalOf... method,
WillAnimateFirstHalfOf can also be used. This method will be called at the beginning of the rotation and the end time. However, in ios6 I found that both methods were discarded, so I used the willAnimateRotationToInterfaceOrientation method to replace the functions of these two methods.
It can also achieve the same effect. It may be replaced by this method officially. I didn't know how to check the information. It's not clear for new users to start learning. The implementation of the willAnimateRotationToInterfaceOrientation method can be implemented as needed.
3. create interfaces for different directions
In some cases, the first two methods are not the best. The third method is to develop different interfaces for different directions. When multiple views need to be created, some views are hidden, so it is 480 in width and 300 in height, but it is found that there is no way to change the size of the view. The book says that you can create a new one, but there is no way to change it. Finally, you can set the horizontal layout here,
However, the results are achieved with the status bar, and there are no other methods to find them.

The final learning result of code end Rotation
-(IBAction) buttonPressed :( id) sender {
NSLog (@ "test ");
If (sender = view1Button1 | sender = view2Button1 ){
View1Button2. hidden = YES;
View2BUtton2. hidden = YES;
} Else {
View1Button1. hidden = YES;
View2Button1. hidden = YES;
}
}
-(Void) willAnimateRotationToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientationduration :( NSTimeInterval) duration {
NSLog (@ "aaa ");
NSLog (@ "% d", toInterfaceOrientation );
NSLog (@ "% d", toInterfaceOrientation = UIInterfaceOrientationPortraitUpsideDown );
If (toInterfaceOrientation = UIInterfaceOrientationPortrait ){
Self. view = view1;
Self. view. transform = CGAffineTransformIdentity;
Self. view. transform = CGAffineTransformMakeRotation (degreeToRadian (0 ));
Self. view. bounds = CGRectMake (0.0, 0.0, 300.0, 460.0 );
NSLog (@ "toportrait ");
} Elseif (toInterfaceOrientation = UIInterfaceOrientationLandscapeLeft ){
NSLog (@ "tolandscapeleft ");
Self. view = view2;
Self. view. transform = CGAffineTransformIdentity;
Self. view. transform = CGAffineTransformMakeRotation (degreeToRadian (-90 ));
Self. view. bounds = CGRectMake (0.0, 0.0, 480.0, 300.0 );
} Elseif (toInterfaceOrientation = UIInterfaceOrientationPortraitUpsideDown ){
NSLog (@ "toupside down ");
Self. view = view1; www.2cto.com
Self. view. transform = CGAffineTransformIdentity;
Self. view. transform = CGAffineTransformMakeRotation (degreeToRadian (180 ));
Self. view. bounds = CGRectMake (0.0, 0.0, 300.0, 460.0 );
} Else {
NSLog (@ "toright ");
Self. view = view2;
Self. view. transform = CGAffineTransformIdentity;
Self. view. transform = CGAffineTransformMakeRotation (degreeToRadian (90 ));
Self. view. bounds = CGRectMake (0.0, 0.0, 480.0, 300.0 );
}
}


-(BOOL) shouldAutorotate {
ReturnYES;
}
-(NSUInteger) supportedInterfaceOrientations {
ReturnUIInterfaceOrientationMaskAll;
}

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.