Ios (ipad, iphone) general method of screen rotation Detection

Source: Internet
Author: User

Ios (ipad, iphone) general method of screen rotation Detection

In special scenarios, screen rotation is needed. It is convenient to implement related functions in ios.

Here are two methods:

1. register the UIApplicationDidChangeStatusBarOrientationNotification notification (for example, register the notification in viewdidload of a viewcontroller class). The sample code is as follows:

[[Nsnotifcencenterdefacenter] addObserver: selfselector: @ selector (statusBarOrientationChange :) name: uiapplicationdidchangestatusbarorientationicationicationobject: nil];

-(Void) statusBarOrientationChange :( NSNotification *) notification

{

UIInterfaceOrientation orientation = [[UIApplicationsharedApplication] statusBarOrientation];

If (orientation = UIInterfaceOrientationLandscapeRight) // right-click the home Key

{

//

}

If (

Orientation = UIInterfaceOrientationLandscapeLeft) // press the home Key to the left

{

//

}

If (orientation = UIInterfaceOrientationPortrait)

{

//

}


If (orientation = UIInterfaceOrientationPortraitUpsideDown)

{

//

}

}

Note that this method listens to StatusBar, that is, the direction of the status bar. Therefore, it is related to your layout. If your layout is changed, you will receive this notification, not the notification of device rotation.

When we focus on things related to Layout rather than purely device rotation, we use the above Code as an implementation solution.

2. register the UIDeviceOrientationDidChangeNotification notification (for example, we also register the notification in viewdidload of the viewcontroller class). The sample code is as follows:

[[Nsicationcenter center defacenter center] addObserver: selfselector: @ selector (orientChange :) name: UIDeviceOrientationDidChangeNotificationobject: nil];

-(Void) orientChange :( NSNotification *) noti

{

NSDictionary * ntfDict = [noti userInfo];

UIDeviceOrientation orient = [UIDevicecurrentDevice]. orientation;

/*

UIDeviceOrientationUnknown,

UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom

UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top

UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right

UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left

UIDeviceOrientationFaceUp, // Device oriented flat, face up

UIDeviceOrientationFaceDown // Device oriented flat, face down */

Switch (orient)

{

CaseUIDeviceOrientationPortrait:

Break;

CaseUIDeviceOrientationLandscapeLeft:

Break;

CaseUIDeviceOrientationPortraitUpsideDown:

Break;

CaseUIDeviceOrientationLandscapeRight:

Break;

Default:

Break;

}

}

Note that the direction in this method also includes face-up or face-down. It is easy to see that this is based entirely on the physical direction of the device itself. When we focus only on the physical orientation, we usually need to register the notification to solve the problem (there is also an api for the accelerator, which can implement similar functions, the api is lower-layer, we recommend that you do not use the above two methods to solve the problem. Improper use causes high performance loss ).




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.