iphone and ipad devices have 4-direction status, and we can adjust the interface to the direction in which the app is currently located.
To enable the application to support different orientations, first we need to set the direction of the device support in the project settings (also can be set in the plist of the project)
Portrait Vertical, Home button at the bottom of the screen
Upside Down, home button on top of screen
Landscape left horizontal, home button on screen
Landscape Right horizontal, home button on screen
We drag a new viewcontroller into the storyboard, bind its file's Owner, then put in 6 UIView, set the different background colors
After the program runs, we are in the rotation simulator, found in the vertical screen and horizontal screen state, the interface is shown as follows
It is obvious that such a display is not suitable for the horizontal screen, and we need to write some code in Viewcontroller in order to get the proper display effect.
First, let's take a look at some of the functions associated with Viewcontroller and gravitational induction.
-(BOOL) shouldautorotate
This function returns Yes to indicate that this view controller supports gravity sensing, and returning no indicates that this view controller does not support gravity sensing
-(uiinterfaceorientation) preferredinterfaceorientationforpresentation
This function sets the direction in which the view controller is first displayed after loading, and uiinterfaceorientation is a struct that supports the following values
Uiinterfaceorientationportrait
Uiinterfaceorientationportraitupsidedown
Uiinterfaceorientationlandscapeleft
Uiinterfaceorientationlandscaperight
-(Nsuinteger) supportedinterfaceorientations
This function sets the direction supported by the view controller (requires Shouldautorotate to return YES), and the supported values are as follows
Uiinterfaceorientationmaskportrait
Uiinterfaceorientationmasklandscapeleft
Uiinterfaceorientationmasklandscaperight
Uiinterfaceorientationmaskportraitupsidedown
Uiinterfaceorientationmasklandscape
Uiinterfaceorientationmaskall = (uiinterfaceorientationmaskportrait | Uiinterfaceorientationmasklandscapeleft | Uiinterfaceorientationmasklandscaperight | Uiinterfaceorientationmaskportraitupsidedown)
Uiinterfaceorientationmaskallbutupsidedown
-(void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: ( Nstimeinterval) Duration
This function is triggered when the user interface is about to rotate, tointerfaceorientation indicates the direction to be reached
-(void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation
This function is triggered at the end of the user interface rotation, frominterfaceorientation indicates the direction before rotation
-(void) Willanimaterotationtointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (nstimeinterval) duration
This function is triggered during the user interface rotation, typically customizing the position and size of the control after the rollover in this function
So in the above example we first link 6 UIView controls to the view controller and then modify the rotated interface in Willanimaterotationtointerfaceorientation:duration:
- (void) Willanimaterotationtointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: ( Nstimeinterval) Duration {if(Uiinterfaceorientationislandscape (tointerfaceorientation)) {Self.view1.frame= CGRectMake ( -, -, the, the); Self.view2.frame= CGRectMake (225, -, the, the); Self.view3.frame= CGRectMake ( -, -, the, the); Self.view4.frame= CGRectMake ( -, the, the, the); Self.view5.frame= CGRectMake (225, the, the, the); Self.view6.frame= CGRectMake ( -, the, the, the); }}
The above code modifies the position of the view control when it is rotated to a horizontal screen, because we strobyboard the vertical interface in the default layout, so there is no need to rearrange the position of the view control in the code, but if we are placing the control in coded form, Then you need to add an else in the code above to set the screen after the vertical.
iOS Gravity sensor