iOS实现全局竖屏,个别页面允许屏幕旋转

第一步: 要实现这种效果必须有这个设置,这个是前提条件哈,

iOS实现全局竖屏,个别页面允许屏幕旋转_第1张图片
配置如上图所示

第二步:在AppDelegate.h的文件中添加属性(如下所示)

@property (assign,nonatomic) BOOL isAllowRevolve;

第三步:在AppDelegate.m的文件中实现代理方法(如下所示)

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

if (self.isAllowRevolve) {

return UIInterfaceOrientationMaskAll;

}

return UIInterfaceOrientationMaskPortrait;

}

好了下面我们看看怎么使用它

第四步:在需要允许的Controller中的viewDidLoad方法中写监听

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(allowRevolve:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

然后实现监听事件

- (void)allowRevolve:(NSNotification*)notify {

if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait

|| [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {

//竖屏

//竖屏操作

} else {

//横屏

//******横屏操作

}

}

第四步:在需要允许的Controller中的viewWillAppear方法中写

-(void)viewWillAppear:(BOOL)animated{

AppDelegate *selfDel = (AppDelegate *)[UIApplication sharedApplication].delegate;

selfDel.allowRotation = YES;

}

最后一步

第五步:一定要在viewWillDisappear的方法里移除监听


-(void)viewWillDisappear:(BOOL)animated

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

AppDelegate *selfDel = (AppDelegate *)[UIApplication sharedApplication].delegate;

selfDel.allowRotation = NO;

//由横屏状态的屏幕返回竖屏屏幕的时候添加以下处理

NSNumber *orientationUnknown = [NSNumber numberWithInt:0];

[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];

NSNumber *orientationTarget = [NSNumber numberWithInt:1];

[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

}

你可能感兴趣的:(iOS实现全局竖屏,个别页面允许屏幕旋转)