如果希望所有界面不可以旋转 则可也在Target —> general 的 device orientation 禁止掉就可以了
或者也可以在AppDelegate中增加,这个方法可以禁止横屏
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
一般我们都是希望某个页面可以选装例如 图片浏览的页面可以横屏,我这里例子图片浏览界面的图片浏览框架出自(code4app mj哥的一个demo)
加速计是整个IOS屏幕旋转的基础,依赖加速计,设备才可以判断出当前的设备方向,IOS系统共定义了以下七种设备方向:
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 };以及如下四种界面方向:
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft }; 我这里整体是一个tabbarController的程序1、定义tabbarController的子类
//
// BaseTabBarViewController.m
// WithBusiness
//
// Created by king on 14-6-18.
// Copyright (c) 2014年 sinosoft. All rights reserved.
//
#import "BaseTabBarViewController.h"
#import "MJPhotoBrowser.h"
#import "BaseNavigationViewController.h"
@interface BaseTabBarViewController ()
@end
@implementation BaseTabBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//6.0之前
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[self getTopViewController] isKindOfClass:[MJPhotoBrowser class]]) {
return [(MJPhotoBrowser*)[self getTopViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
//禁止旋转
return (interfaceOrientation==UIInterfaceOrientationPortrait);
}
//6.0之后 包括6.0
- (BOOL)shouldAutorotate
{
if ([[self getTopViewController] isKindOfClass:[MJPhotoBrowser class]]) {
return YES;
//这里我直接return yes 也可也return 下面这行代码 由topViewController来做控制
// return [[self getTopViewController] shouldAutorotate];
}
return NO;
}
//
- (NSUInteger)supportedInterfaceOrientations
{
if ([[self getTopViewController] isKindOfClass:[MJPhotoBrowser class]]) {
return [(MJPhotoBrowser*)[self getTopViewController] supportedInterfaceOrientations];
}
//返回UIDeviceOrientationPortraitUpsideDown, 当在图片浏览界面横屏返回上一页面的时候会自动恢复到竖屏
return UIDeviceOrientationPortraitUpsideDown;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
return [[self getTopViewController] willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
//获取最顶层的ViewController
- (id)getTopViewController{
BaseNavigationViewController *navCtr = (BaseNavigationViewController*)self.selectedViewController;
BaseUIViewController *viewController = (BaseUIViewController *)navCtr.topViewController;
return viewController;
}
@end
在mjBrowser的类里面
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight
||[[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
||[[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft){
return YES;
}
return NO;
}
- (BOOL)shouldAutorotate
{
if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight
||[[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
||[[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft){
return YES;
}
return NO;
}
要旋转的时候会调用这个方法
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
可以里面做一些横屏操作
判断是否横屏:
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight
||[[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft) {
_screen_change = YES;
}