IOS: screen rotation and transform

Source: Internet
Author: User

IOS: screen rotation and transform

 

The itouch, iPhone, and iPad settings support rotation.ProgramYou can make different la s based on different directions, and the experience will be better.

How can we set the program to support rotation? We usually set it in info. plist of the program.Supported interface orientations, Add the direction to be supported by our program, and each viewcontroller in the program also has a method

Supportedinterfaceorientations (later than 6.0)

Shouldautorotatetointerfaceorientation (system before 6.0)

Through these viewcontroller methods, we can achieve a smaller granularity of rotation control, for example, the program only allows individual interface rotation.

 

I,What exactly does screen rotation do?

The following is a simple example.XcodeCreate a default Single View project, and thenViewcontrollerOutput the current view information in the function after the response is rotated,CodeAs 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 any additional setup after loading the view, typically from a nib.  Self. View. backgroundcolor = [Uicolor graycolor];} -( Void  ) Didreceivememorywarning {[Super didreceivememorywarning];  //  Dispose of any resources that can be recreated.  } -(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {  Return  Yes ;} - (Bool) shouldautorotate {  Return  Yes ;} - (Nsuinteger) supportedinterfaceorientations {  Return  Uiinterfaceorientationmaskall ;} -( Void  ) Willrotatetointerfaceorientation :( uiinterfaceorientation) tointerfaceorientation duration :( nstimeinterval) Duration {nslog ( @"  Uiviewcontroller will rotate to orientation: % d  "  , Tointerfaceorientation );} -( Void  ) Didrotatefrominterfaceorientation :( uiinterfaceorientation) frominterfaceorientation {nslog (  @"  Did rotated to new orientation, view information % @  "  , Self. View );}  @ End 

Check the code and we can find that ourViewcontrollerSupports four directions, andDidrotatefrominterfaceorientationThe function printsSelf. ViewWe can see the following output by rotating a circle:

The device's initial direction isUiinterfaceorientationportraitAnd then go through clockwiseLandscapeleft,Portraitupsidedown,Landscaperight,ReturnUiinterfaceorientationportraitDirection. If you take a closer look, we will find thatFrameBesides,TransformIt is also changing. ObservationFrameIt is found that its changes should be caused by the status bar of the system. So the system status bar is hidden and found in the outputFrameIt does not change. So we can suspect that the screen rotation is changed throughTransform.

 

2. What isTransform

Transform(Change matrix) is a 3 × 3 matrix, as shown in:

Through this matrix, we can scale, translate, rotate, and group operations on a coordinate system. In addition, matrix operations do not have an exchange law, that is, the order of matrix operations may lead to different results.UiviewThere isTransformBy setting this property, we can adjust thisViewIn itsSuperview.

Mathematical Knowledge behind matrix implementation of coordinate changes:

Let X and Y represent the positions in the original coordinate system respectively. X' and y' represent the positions in the new system after matrix changes. Formula 1 is the formula for matrix change. After Formula 1 is expanded, formula 2 is obtained. From formula 2, we can clearly see the changing relationship between (x, y) and (x', y.

1) when C, B, TX, and ty are all zero, x' = Ax, y' = by; that is, A and D represent the scaled-up ratio in the X and Y directions respectively; when both A and D are 1, x' = x, y' = y; at this time, this matrixThat is, the legendaryCgaffinetransformidentity(Standard matrix ).

2) When a, d is 1, C, and D is zero, x' = x + Tx, y' = Y + ty; that is, TX and Ty represent X, the translation distance in the Y direction.

3)In the first two cases, we can achieve scaling and shifting. How can we represent rotation?

Assuming that the translation and scaling operations are not performed, after a moment (x, y) in the original coordinate system rotates α ° to a point (x', y') in the new coordinate system, the rotation matrix is as follows:

After expansion, it is x' = xcos α-Ysin α, y' = xsin α + ycos α;

 

In practical application, we can combine these changes to complete all two-dimensional matrix changes. Now let's look back at the output of the previous device rotation, when the device is locatedPortraitBecause the Matrix is a standard matrix, it is not printed. WhenUiinterfaceorientationlandscapeleftIn the direction, our device turns clockwise to 90 °. At this time, the matrix should be(Cos90 °, sin90 °,-sin90 °, cos90 °, TX, Ty)Because no translation operation is performed, the Tx and Ty values are both 0, which can be output in the console :"<Uiview: 0x8075390; frame = (0 0; 320 480); transform = [0,-1, 1, 0, 0, 0]; autoresize = W + h; layer = <calayer: 0x8074980>"Consistent. Observe the output in the other two directions and find that the results are consistent with those in the analysis.

as a result, you can see that screen rotation is actually implemented through matrix changes of views, when the device detects rotation, the current program is notified. The current program then notifies window , window notifies rootviewcontroller , rootviewco View transform to complete the rotation.

If we directlyViewAddWindow, The system will not help us complete the spin operation, at this time we need to set thisViewOfTransformTo achieve rotation. This situation is rare, but it also exists. For example, the function of many apps to use the status bar for message prompting is to create a message by themselves.WindowAnd set it by yourselfTransformThe next blog will introduce how to implement this message notification.

 

Note: Please indicate the source for reprinting! Welcome to join me in QQ 1592232964 to discuss common progress.

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.