屏幕强转实现

来源:互联网 发布:淘宝网睡衣清仓特价 编辑:程序博客网 时间:2024/05/01 22:43
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        }- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];}- (void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];            [[NSNotificationCenter defaultCenter]removeObserver:self];}- (IBAction)orientationAction:(id)sender {        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;    switch (interfaceOrientation) {        case UIInterfaceOrientationPortraitUpsideDown:{ // 电池栏在下                        [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];                        break;        }        case UIInterfaceOrientationLandscapeLeft:{ // 电池栏在右                        [self interfaceOrientation:UIInterfaceOrientationPortrait];                        break;        }                    case UIInterfaceOrientationLandscapeRight:{ // 电池栏在左                        [self interfaceOrientation:UIInterfaceOrientationPortrait];                        break;        }        case UIInterfaceOrientationPortrait:{ // 电池栏在上                        [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];                        break;        }                    default:            break;    }}// 强制转屏相关- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{    // arc下    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {        SEL selector = NSSelectorFromString(@"setOrientation:");        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];        [invocation setSelector:selector];        [invocation setTarget:[UIDevice currentDevice]];        int val = orientation;        [invocation setArgument:&val atIndex:2];        [invocation invoke];    }}// 开始转屏---------- (void)orientationChange{        }@end









0 0
原创粉丝点击