ios自动(手动)转屏

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

一、第一次做转屏的时候走了不少弯路,过一段时间不写,发现忘了差不多了,还好有度娘和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 {

[superviewWillAppear:animated];

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

del.orientation=UIInterfaceOrientationMaskAllButUpsideDown;

}

③在需转屏的viewController加

- (NSUInteger)supportedInterfaceOrientations {

returnUIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

}

- (BOOL)shouldAutorotate {

returnYES;

}

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

1、假转屏

①转状态栏

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

②旋转当前view

self.view.transform = CGAffineTransformRotate(self.view.transform, M_PI/4);

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

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

UIInterfaceOrientationorientation = orientate.intValue;

if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {

[[UIDevicecurrentDevice]performSelector:@selector(setOrientation:)withObject:(id)orientation];

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航