首先添加通知, 记得在控制器销毁时移除通知就可以了, 不同方向需要实现的代码直接下载switch case 语句中就可以了
//
// ViewController.m
// 屏幕方向检测
//
// Created by Wangjunling on 16/4/6.
// Copyright © 2016年 Wangjunling. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加屏幕方向改变的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationDidChange {
switch ([UIDevice currentDevice].orientation) {
case 0:
NSLog(@"UIDeviceOrientationUnknown");//默认
break;
case 1:
NSLog(@"UIDeviceOrientationPortrait");//垂直向上
break;
case 2:
NSLog(@"UIDeviceOrientationPortraitUpsideDown");//垂直向下
break;
case 3:
NSLog(@"UIDeviceOrientationLandscapeLeft");//垂直向左
break;
case 4:
NSLog(@"UIDeviceOrientationLandscapeRight");//垂直向右
break;
case 5:
NSLog(@"UIDeviceOrientationFaceUp");//平放屏幕向上
break;
case 6:
NSLog(@"UIDeviceOrientationFaceDown");//平放屏幕朝下
break;
default:
break;
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end