Screen direction judgment during ios development
The most effective method of Landscape Portrait screen Portrait Landscape screen is to store the orientation in willRotateToInterfaceOrientation: duration: Method: DrviceOrientation = toInterfaceOrientation; in other ways, use the corresponding screen Direction Method 1: directly obtain the device method: self. interfaceOrientation (this method has expired) Method 2: Use the following method: UIDeviceOrientation duration = [[UIDevice currentDevice] orientation]; Method 1 and 2 when running in the simulator, the device direction obtained at the beginning is UnKnow method 3. Calculate the direction of the Screen Based on the screen width [UIScreenmainScreen] applicationFrame]. size. height [[UIScreenmainScreen] PplicationFrame]. size. width can be used to obtain the size, height, and width of the current screen. Because the system status bar occupies 20 and is always above the screen, it changes the two values above, so you can determine whether the current screen is landscape or landscape. To put it simply, the height is 1004, and the width is 768. When the screen is landscape, the height is 1024, and the width is 748. Of course, the premise is that you did not remove the status bar of the system. it can be used in any method as a judgment condition. application Example: if (loadingview = nil) {loadingview = [[UIViewalloc] initWithFrame: CGRectMake (284,402,200,200)]; if ([[UIScreenmainScreen] applicationFrame]. size. height = 1024) {loadingview. frame = CGRectMake (412,264,200,200); // This is a horizontal screen} [loadingviewsetBackgroundColor: [UIColorclearColor];} // set the position based on the current horizontal and vertical screen when creating loadingview. Method 4 someone has posted a message in the Forum (I only do some simple operations here) // The following is a direct judgment in the screen direction-(void) willAnimateRotationToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation duration :( NSTimeInterval) duration {switch (interfaceOrientation) {caseUIInterfaceOrientationPortrait: // The home is under break; then: // The home is on the left break; caseUIInterfaceOrientationLandscapeRight: // The home is located in the right break; default: break ;} method 5: when the screen is rotated, record the screen orientation. The screen needs to be rotated once after the window is started, otherwise there will be a problem with the screen direction when the program starts # pragma mark screen rotation completed-(void) didRotateFromInterfaceOrientation :( UIInterfaceOrientation) fromInterfaceOrientation {NSLog (@ "screen rotation completed dockView height % f", self.doc kView. height); // self.doc kView. width = self.doc kWidth;} # The pragma mark screen will rotate-(void) duration :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {[UIView duration: 0.25 animations: ^ {self.doc kView. width = self.doc kWidth;}]; // determine whether the screen is landscape or landscape (if (UIInterfaceOrientationIsPortrait (toInterfaceOrientation) {NSLog (@ "screen will be rotated to landscape"); self.doc kView. width = self.doc kWidth;} else {NSLog (@ "screen will be rotated to landscape"); self.doc kWidth = 100 ;}}