首先Device orientation中要配置支持方向
其次關(guān)鍵方法如下:
1.是否支持自動(dòng)旋轉(zhuǎn)
- (BOOL)shouldAutorotate
2.返回支持旋轉(zhuǎn)的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
支持正立豎屏
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
支持左橫屏
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
支持右橫屏
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
支持倒立豎屏
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
支持倒立橫屏
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
支持全部
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
除了倒立豎屏都支持
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
3.即將旋轉(zhuǎn)時(shí)代理回調(diào)
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
4.完成旋轉(zhuǎn)時(shí)代理回調(diào)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
當(dāng)轉(zhuǎn)動(dòng)屏幕后,手機(jī)會(huì)根據(jù)旋轉(zhuǎn)方向進(jìn)行主動(dòng)旋轉(zhuǎn)侈百。
被動(dòng)強(qiáng)制屏幕旋轉(zhuǎn):
如果想要強(qiáng)制旋轉(zhuǎn)屏幕,比如點(diǎn)擊按鈕觸發(fā)式旋轉(zhuǎn)藕届,方法如下:
(但這個(gè)方法不太親和,可能會(huì)被拒或者與自動(dòng)旋轉(zhuǎn)沖突潛在bug)
NSNumber *resetOrientationTarget = [NSNumber numberWithInt: UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue: resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt: UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue: orientationTarget forKey:@"orientation"];