The problem of screen direction judgment during iOS development

Source: Internet
Author: User

  Landscape Vertical Screen Portrait horizontal screen The most effective way is: in Willrotatetointerfaceorientation:duration: method to store the direction: Drviceorientation = Tointerfaceorientation; and then in another way, use the corresponding screen direction method one: The method of obtaining the device directly: Self.interfaceorientation (this method has expired) Method Two: The following method: uideviceorientation duration = [[Uidevice currentdevice]orientation]; methods 1, 2 when running in an emulator, The device is just starting to get the direction of Unknow method Three according to the width of the screen to calculate the corresponding screen direction [[Uiscreenmainscreen] applicationframe].size.height[[uiscreenmainscreen] Applicationframe].size.width can be used to get the current screen size, height and width. Because the system's State bar is 20 higher and always above the screen, it makes the above two values change in the vertical screen, so it can be used to determine whether the current is a horizontal or an upright screen. Simply put, the height of the vertical screen is 1004,width to 768. When horizontal screen, height is 1024,width to 748. Of course, the prerequisite is that you do not remove the system's status bar. It can be used in any method as a condition of judgment. The following examples are applied: if (Loadingview ==nil) {loadingview = [[Uiviewalloc] initWithFrame: CGRectMake (284, 402, 200, 200)]; if ([[[Uiscreenmainscreen] applicationframe].size.height==1024) {loadingview.frame=cgrectmake (412, 264, 200, 200);/ Horizontal screen now} [Loadingviewsetbackgroundcolor:[uicolorclearcolor]]; When creating the Loadingview, set the position according to the current horizontal screen. Method Four in the forum has been sent (I only do a simple handling work here)//below is directly in the direction of the screen to judge-(void) WillanimaterotationtointerfaCeorientation: (uiinterfaceorientation) interfaceorientation Duration: (nstimeinterval) duration {switch ( interfaceorientation) {caseuiinterfaceorientationportrait://home alive under the break; Caseuiinterfaceorientationportraitupsidedown://home alive on the break; Caseuiinterfaceorientationlandscapeleft://home alive left break; Caseuiinterfaceorientationlandscaperight://home Alive Right break; Default:break; Method five again when the screen is rotated to record the direction of the screen to do the painting needs to be a window after the start of the screen to rotate, otherwise the painting when the program started when the direction of the screen will be a problem #pragma mark screen rotation complete-(void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation{NSLog (@ "screen rotation completes Dockview height%f ", self.dockView.height); Self.dockView.width = Self.dockwidth;} #pragma mark screen will rotate-(void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation duration :(nstimeinterval) duration{[UIView animatewithduration:0.25 animations:^{self.dockView.width = self.dockwidth;}];// To determine whether the vertical screen if (uiinterfaceorientationisportrait (tointerfaceorientation)) {NSLog (@ "screen will be rotated to an upright screen"); self.dockView.width = Self.doCkwidth; }else{NSLog (@ "screen will rotate to horizontal screen"); self.dockwidth = 100;}}

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.