The iPhone screen is 320*480, And the status bar height is 20 pixels. It mainly displays power, signal strength, and time.
Applications generally use three methods to rotate the screen:
1. automatically adjust attributes
Ii. Rebuilding the view during rotation
3. Switch between multiple views
1. Automatically Adjusting attributes and rebuilding views during rotation can be merged into one method, which is not suitable for complicated views. The two key functions used are:
-(Bool) shouldautorotatetointerafceorientation :( uiinterfaceorientation) interfaceorientation {
Return yes;
};
This function is used to determine the rotation direction supported by our application. If you want to support each direction, yes is returned directly. You can also determine a direction separately:
-(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation { If (interfaceorientation = uiinterfaceorientationlandscapeleft ){ // Left } If (interfaceorientation = uiinterfaceorientationlandscaperight ){ // Right } If (interfaceorientation = uiinterfaceorientationportrait ){ // Up } If (interfaceorientation = uiinterfaceorientationportraitupsidedown ){ // Down } Return yes; } |
In the above function, uiinterfaceorientationlandscapeleft, uiinterfaceorientationlandscaperight, uiinterfaceorientationportrait, and uiinterfaceorientationportraitupsidedown are four macros, which represent left, right, inverted, and normal.
If only this function returns yes in the corresponding direction, the control layout may be poor. You also need to use interface builder to adjust the Automatic Layout attribute, as shown in
The specific animation setting method is omitted.
Of course, there are some functions that can be triggered by rotation:
// When the rotation direction changes -(Void) willanimaterotationtointerfaceorientation :( uiinterfaceorientation) tointerfaceorientation duration :( nstimeinterval) Duration { } // Automatically called before the first half of the view rotation animation occurs -(Void) willanimatefirsthalfofrotationtointerfaceorientation :( uiinterfaceorientation) tointerfaceorientation duration :( nstimeinterval) Duration { } // Automatically called before the last half of the view rotation animation occurs -(Void) willanimatesecondhalfofrotationfrominterfaceorientation :( uiinterfaceorientation) frominterfaceorientation duration :( nstimeinterval) Duration { } // Automatically called before view Rotation -(Void) willrotatetointerfaceorientation :( uiinterfaceorientation) tointerfaceorientation duration :( nstimeinterval) Duration { } // Automatically called after view rotation is complete. It is generally used to re-enable some controls or suspend activities before turning over, such as resuming video playback. -(Void) didrotatefrominterfaceorientation :( uiinterfaceorientation) frominterfaceorientation { } // Automatically calls the first half of a view rotation animation after it occurs. -(Void) didanimatefirsthalfofrotationtointerfaceorientation :( uiinterfaceorientation) tointerfaceorientation { } |
We can rewrite the willanimaterotationtointerfaceorientation: Duration: function to adjust the layout by hard encoding when rotation occurs, mainly to modify the frame attribute of the control, which is a cgrect structure, you can use the cgrectmake function provided by Apple to specify the X, Y coordinates, and the width and heigh attributes.
-(Void) Duration :( uiinterfaceorientation) tointerfaceorientation duration :( nstimeinterval) Duration {If (tointerfaceorientation = duration) {button1.frame = cgrectmake ); // button1 is a control output port} else {button1.frame = cgrectmake );}}
The second method is to create multiple views. When the screen is rotated, you can switch between view1 and view2. this method is suitable for complex interfaces, when the controls in the two views may trigger the same operation, we must provide two different output port sets for the two views, which correspond to the two views respectively, which increases the complexity of the Code, view1 and view2 are assigned to view attributes of the View Controller respectively, and two view output ports must be provided for the two new views in the View Controller, in IB, they are connected to the corresponding view. iboultet uiview * view1 and iboultet uiview * view2 are also modified in the willanimaterotationtointerfaceorientation: Duration: function, and must be self. view. transform and self. view. the bounds attribute selection value is as follows:
# Define degreetoradians (x) (m_pi * x/180) // defines a macro to convert degrees to radians.
// After the rotation starts, this function is called before the actual rotation-(void) Duration :( uiinterfaceorientation) tointerfaceorientation duration :( nstimeinterval) Duration {If (tointerfaceorientation = uiinterfaceorientationportrait) {self. view = self. view1; self. view. transform = cgaffinetransformmakerotation (degreetoradians (0); self. view. bounds = cgrectmake (0.0, 0.0, 320.0, 460.0);} else if (tointerfaceorientation = uiinterfaceorientationlandscapeleft) {self. view = self. view2; self. view. transform = cgaffinetransformmakerotation (degreetoradians (-90); self. view. bounds = cgrectmake (0.0, 0.0, 480.0, 300.0);} else if (tointerfaceorientation = uiinterfaceorientationportraitupsidedown) {self. view = self. view1; self. view. transform = cgaffinetransformmakerotation (degreetoradians (180); self. view. bounds = cgrectmake (0.0, 0.0, 320.0, 460.0);} else if (tointerfaceorientation = uiinterfaceorientationlandscaperight) {self. view = self. view2; self. view. transform = cgaffinetransformmakerotation (degreetoradians (90); self. view. bounds = cgrectmake (0.0, 0.0, 480.0, 300.0 );}}