IOS 自动(手动)转屏

一、第一次做转屏的时候走了不少弯路,过一段时间不写,发现忘了差不多了,还好有度娘和google,让我很快找到感觉,下面来谈谈我对转屏的了解(有不对的地方或更好的方法请留言,不胜感激!!!)

iOS6前的转屏比较简单就一个方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

    return (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation ==UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);

}

 

iOS6及以后

①告知Appdelegate要支持转屏的方向

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

    returnself.orientation;

}

 

②在需转屏的viewController

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    AppDelegate *del = (AppDelegate *)[UIApplicationsharedApplication].delegate;

    del.orientation = UIInterfaceOrientationMaskAllButUpsideDown;

}

 

③在需转屏的viewController加

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

}

- (BOOL)shouldAutorotate {

    returnYES;

}

 

二、手动转屏(按一个按钮或者一个事件触发)

1、假转屏

①转状态栏

[[UIApplication sharedApplication]  setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

②旋转当前view

self.view.transform = CGAffineTransformRotate(self.view.transformM_PI/4);

 

2、用私有方法转屏(此类要在ARC),上Appstore慎用

- (void)setCustomOrientation:(NSString *)orientate {

    UIInterfaceOrientation orientation = orientate.intValue;

     if ([[UIDevice currentDevicerespondsToSelector:@selector(setOrientation:)]) {

        [[UIDevice currentDeviceperformSelector:@selector(setOrientation:) withObject:(id)orientation];

    }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值