iOS 屏幕旋转问题

如果希望所有界面不可以旋转 则可也在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;
            
        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值