How to use the screen rotation function in IOS development _ios

Source: Internet
Author: User
Tags cos sin uikit

The accelerometer is the basis for the entire iOS screen rotation, depending on the accelerometer, the device can determine the current device direction, the iOS system defines the following seven device orientations:

Copy Code code as follows:

typedef ns_enum (Nsinteger, uideviceorientation) {

Uideviceorientationunknown,

Uideviceorientationportrait,//Device oriented vertically, home button on the bottom

Uideviceorientationportraitupsidedown,//Device oriented vertically, home button on the top

Uideviceorientationlandscapeleft,//Device oriented horizontally, home button on the right

Uideviceorientationlandscaperight,//Device oriented horizontally, home button on the left

Uideviceorientationfaceup,//Device oriented flat, face up

Uideviceorientationfacedown//Device oriented flat, face down

};



As well as the following four kinds of interface direction:
Copy Code code as follows:

typedef ns_enum (Nsinteger, uiinterfaceorientation) {

uiinterfaceorientationportrait = uideviceorientationportrait,

Uiinterfaceorientationportraitupsidedown = Uideviceorientationportraitupsidedown,

Uiinterfaceorientationlandscapeleft = Uideviceorientationlandscaperight,

Uiinterfaceorientationlandscaperight = Uideviceorientationlandscapeleft

};



One, Uikit processing screen rotation process

When the accelerometer detects a change in direction, a uideviceorientationdidchangenotification notification is issued, so that any view that is interested in the direction of the change can register the notice and respond accordingly when the device direction changes. In the previous blog, we've mentioned that when the screen spins, Uikit helps us do a lot of things to make it easier for us to do the screen rotation.

Uikit's corresponding screen rotation process is as follows:

1, when the equipment rotates, Uikit receives the rotation event.

2, Uikit through the Appdelegate notify the current program window.

3, window will know its rootviewcontroller, judge the view controller the direction of rotation supported, complete rotation.

4, if there is a pop-up view controller, the system will be based on the pop-up view controller to determine whether to rotate.



Second, uiviewcontroller to achieve screen rotation

In response to the device rotation, we can achieve finer granularity of control through the Uiviewcontroller method, when the view controller receives a change in the direction of the window, the process is as follows:

1, first of all to determine whether the current Viewcontroller support rotation to the target direction, if support to enter the flow of 2, otherwise this rotation process directly end.

2, Call Willrotatetointerfaceorientation:duration: Method, notify view controller will rotate to the target direction. If the Viewcontroller is a container view controller, it continues to invoke the method of its content view controller. This time we can also temporarily hide some of the view, and so on after the rotation of the reality.

3, window adjustment display view controller bounds, because the view controller bounds changes, will trigger the Viewwilllayoutsubviews method. This time self.interfaceorientation and statusbarorientation direction is still the original direction.

4, then the current View controller willanimaterotationtointerfaceorientation:duration: method will be called. All the property changes performed in the method will be placed in the dynamic animation block.

5. Rotate the animation in the direction of execution.

6, the Last Call Didrotatefrominterfaceorientation: method, notify view controller rotation animation completed. This time we can show the second hidden view again.

The entire response process is shown in the following illustration:

The above is Uikit a complete screen rotation process, we need to follow the prompts to make the appropriate processing can be perfect support screen rotation.



Iii. matters of concern and recommendations

1) Matters needing attention

When our view controller is hidden, the direction of the device may also change. For example, when View Controller a pops up a full-screen view Controller B, the screen rotation message is not received because a is completely invisible. This time if the screen direction changes, and then dismiss B, the direction of a will be incorrect. We can fix the problem by updating the direction in the Viewwillappear of view controller A.

2 Some suggestions when the screen spins
• In the rotation process, the temporary interface operation response.
• Rotate before and after, as far as possible the current display position unchanged.
• For more complex view hierarchies, use screenshots to replace the current view level before the rotation starts, and then replace the original view level with the rotation at the end.
• It is best to force reload TableView after rotation to ensure that the new row fills the full screen after changing direction. For example, for some photo display interface, vertical screen display only one column, but the screen display list interface, this time an interface will show more elements, at this point reload content is very necessary.

IOS: Screen rotation and transform
Itouch,iphone,ipad settings are supported by rotation, and if our program can make different layouts according to different directions, the experience will be better.

How to set the program to support rotation, usually we will set up in the info.plist of the program supported interface orientations, add our program to support the direction, and the program inside each viewcontroller also have a way

Supportedinterfaceorientations (6.0 and beyond)

Shouldautorotatetointerfaceorientation (System prior to 6.0)

By Viewcontroller These methods, we can achieve a smaller size of the rotation control, such as the program allows only the individual interface rotation.




What is behind the screen rotation?

Let's look at a simple example, create a new default single view project with Xcode, and then output the current view information in the corresponding Viewcontroller response rotation function, the code is as follows:

Copy Code code as follows:

Svrotateviewcontroller

//
Svrotateviewcontroller.m
Svrotatebytransform
//
Created by Maple on 4/21/13.
Copyright (c) 2013 Maple. All rights reserved.
//

#import "SvRotateViewController.h"

@interface Svrotateviewcontroller ()

@end

@implementation Svrotateviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.

Self.view.backgroundColor = [Uicolor Graycolor];
}

-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}

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

-(BOOL) shouldautorotate
{
return YES;
}

-(Nsuinteger) supportedinterfaceorientations
{
return uiinterfaceorientationmaskall;
}

-(void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (NSTimeInterval ) duration
{
NSLog (@ "Uiviewcontroller'll rotate to orientation:%d", tointerfaceorientation);
}

-(void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation
{
NSLog (@ "did rotated to new orientation, view information%@", Self.view);
}

@end

Looking at the code we can find that our Viewcontroller supports four directions and then prints the Self.view information in the didrotatefrominterfaceorientation function of the rotation, and we can see the following output in a round rotation:

Second, what is transform

The Transform (change matrix) is a 3x3 matrix, as shown in the following illustration:

This matrix allows us to scale, translate, rotate, and manipulate any group of coordinate systems. Moreover, the operation of matrices does not have commutative law, that is, the order of matrix operation can result in different results. UIView has a transform attribute that allows us to resize and position the view in its Superview by setting this property.

The mathematical knowledge behind the coordinate variation of the matrix is realized:

Set the x,y to represent the position in the original coordinate system, X ', y ' represents the position of the new system after the change of matrix. The Formula 1 is the matrix change equation, the 1 to be expanded after the 2. From Type 2 We can clearly see the change relationship (X,y) to (x ', Y ').

1 when the c,b,tx,ty is zero, the x ' = Ax,y ' = by; that is, the a,d represents the proportional magnification on behalf of the x,y direction respectively; When A,d is 1 o'clock, x ' = X,y ' = y This time the matrix is also the legendary cgaffinetransformidentity (standard matrix).

2 when the a,d is 1,c,b zero, x ' = x + tx,y ' = y + ty; that is, tx,ty represents the translation distance in the x,y direction respectively.

3 The first two situations can achieve scaling and peace shift, then how to represent the rotation?

Assuming that the translation and scaling operations are not done, then rotate the α° from one point (X,y) in the original coordinate system to a point (X ', y ') in the new coordinate system, the rotation matrix is as follows:

After unfolding is x ' = xcosα-ysinα,y ' = xsinα+ ycosα;



In practical applications, we combine these changes to complete all two-dimensional matrix changes. Now we're looking back at the output from the front device rotation, when the device is in portrait, because the matrix is a standard matrix, so there is no printing. When we turn to the uiinterfaceorientationlandscapeleft direction, our equipment is clockwise (counterclockwise is positive, clockwise is negative), this time the matrix should be (cos-90°,sin-90°,-sin-90°, Cos-90°,tx,ty), because the tx,ty are 0 because they are not panning, we can just output it to our console: "<UIView:0x8075390; frame = (0 0; 320 480); Transform = [0,-1, 1, 0, 0, 0]; AutoResize = w+h; Layer = <CALayer:0x8074980>> "consistent. The output of the other two directions was observed and the results were consistent with the analysis.

This can be found that the screen rotation is actually through the view of the matrix changes, when the device to monitor the rotation, will notify the current program, the current program to notify the program Window,window will notify its rootviewcontroller, Rootviewcontroller sets the transform of its view, and finally completes the rotation.

If we add a view directly to the window, the system will not help us to complete the spin operation, and this time we need to set the view of the transform to achieve the rotation. This is relatively small, but there are, for example, many apps do using the status bar to use the message hint function is to use their own create windows and set up their own transform to complete the rotation support, the next blog will explain how to implement this message notification.


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.