Code snippets for iOS device Orientation

Source: Internet
Author: User

From the perspective of IOS code snippets, the technology is quite convenient and reprinted here.

In xcode4, you can easily configure the IOS Project to Support Device orientation in the project property settings,

Unfortunately, this setting only stores related settings in plist. If you really want to control the device flip support for a uiview, You have to toss in the related uiviewcontroller-shouldautorotatetointerfaceorientation: function, yes or no is returned Based on the device direction.

This code snippet simplifies related operations. With this code, you can directly query plist settings in the shouldautorotatetointerfaceorientation function, and return results based on the settings, instead of making one-to-one judgments using manual code.

static inline NSString *    NSStringFromUIInterfaceOriention(UIInterfaceOrientation orientation){    switch (orientation) {        case UIInterfaceOrientationPortrait:            return @"UIInterfaceOrientationPortrait";        case UIInterfaceOrientationPortraitUpsideDown:            return  @"UIInterfaceOrientationPortraitUpsideDown";        case UIInterfaceOrientationLandscapeLeft:            return @"UIInterfaceOrientationLandscapeLeft";        case UIInterfaceOrientationLandscapeRight:            return @"UIInterfaceOrientationLandscapeRight";        default:            return @"Unexpected";    }}static inline BOOL UIInterfaceOrientationIsSupportedOrientation(UIInterfaceOrientation interfaceOrientation){    NSArray *array = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];    NSUInteger index = [array indexOfObject:NSStringFromUIInterfaceOriention(interfaceOrientation)];    return index != NSNotFound;}

 

Use shouldautorotatetointerfaceorientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return UIInterfaceOrientationIsSupportedOrientation(interfaceOrientation);}

 

And: some people hate global functions. You can also consider blocking them into uiviewcontroller as a category. Adding self to the call is quite elegant.

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.