ios一句代碼搞定屏幕旋轉

咱們項目中常常用到屏幕旋轉功能,爲此我封裝了一個文件,但願能幫助你們。



 .h文件:

#import <UIKit/UIKit.h>ide

@interface LSLRotationManager : NSObject atom

@property (nonatomic, readonly) UIInterfaceOrientationMask interfaceOrientationMask; @property (nonatomic) UIDeviceOrientation orientation; spa

 + (instancetype)defaultManager;cdn

 @end blog

 .m文件  

#import "LSLRotationManager.h"it

#import <objc/message.h>io


static LSLRotationManager *INSTANCE = nil;class


@interface LSLRotationManager ()import


@property (nonatomic, readwrite) UIInterfaceOrientationMask interfaceOrientationMask;lazyload


@end


@implementation LSLRotationManager


#pragma mark - singleton

+ (instancetype)defaultManager {

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

INSTANCE = [[super allocWithZone:nil] init];

INSTANCE.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;

});

return INSTANCE;

}


+ (instancetype)allocWithZone:(struct _NSZone *)zone {

return [self defaultManager];

}


#pragma mark - setter methods

- (void)setOrientation:(UIDeviceOrientation)orientation {

if (_orientation == orientation) return;

if ([UIDevice currentDevice].orientation == orientation) {

//強制旋轉成與以前不同的

[[UIDevice currentDevice] setValue:@(_orientation) forKey:@"orientation"];

}

_orientation = orientation;

UIInterfaceOrientationMask interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;

switch (orientation) {

case UIDeviceOrientationPortrait:

interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;

break;

case UIDeviceOrientationPortraitUpsideDown:

interfaceOrientationMask = UIInterfaceOrientationMaskPortraitUpsideDown;

break;

case UIDeviceOrientationLandscapeRight:

interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeLeft;

break;

case UIDeviceOrientationLandscapeLeft:

interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;

break;

default:

interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;

break;

}

[LSLRotationManager defaultManager].interfaceOrientationMask = interfaceOrientationMask;

//強制旋轉成全屏

[[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"];

}


調用的時候調用

 [LSLRotationManager defaultManager].orientation = UIDeviceOrientationLandscapeLeft

就ok了 

相關文章
相關標籤/搜索