0%

iOS 编程:UIDeviceOrientation 设备方向

UIDeviceOrientation

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    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
}

示例:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 通知中心是专门供程序中,不同类间的消息通信而设置的。这里用来捕捉手机方向切换的事件
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
}

- (void)orientationChanged {
    switch ([[UIDevice currentDevice] orientation]) {
        case UIDeviceOrientationPortrait:
            NSLog(@"设备垂直,Home按钮在底部!");
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"设备垂直,Home按钮在顶部!");
            break;
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"设备水平,Home按钮在右边!");
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"设备水平,Home按钮在左边!");
            break;
        case UIDeviceOrientationFaceUp:
            NSLog(@"设备平躺,正面朝上!");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"设备平躺,正面朝下!");
            break;
        default:
            break;
    }
}

欢迎关注我的其它发布渠道