iOS屏幕旋转

UIInterfaceOrientation方向枚举:
UIInterfaceOrientationPortrait //home健在下
UIInterfaceOrientationPortraitUpsideDown //home健在上
UIInterfaceOrientationLandscapeLeft //home健在左
UIInterfaceOrientationLandscapeRight //home健在右

旋转屏幕时触发的函数:
//旋转方向发生改变时
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转动画前一半发生之前自动调用
-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转动画后一半发生之前自动调用
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转之前自动调用
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转完成之后自动调用
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}
//视图旋转动画前一半发生之后自动调用
-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
}

如果项目中用了navigationViewController, 那么就应该新建一个uinavigationViewController的子类,然后在这个类里面写上下面的代码,在使用的时候就用自定义的这个navCtr, 就是说需要在根视图里面控制

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
return toInterfaceOrientation != UIDeviceOrientationPortraitUpsideDown; 
} 
 - (BOOL)shouldAutorotate { 
if ([self.topViewController isKindOfClass:[AddMovieViewController class]]) {       // 如果是这个 vc 则支持自动旋转 
return YES; 
} 
return NO; 
} 
- (NSUInteger)supportedInterfaceOrientations { 
return UIInterfaceOrientationMaskAllButUpsideDown; 
}

1.Window级别的控制
对于UIApplicationDelegate的这个方法声明,大多数情况下application就是当前的application,而window通常也只有一个。所以基本上通过window对横屏竖屏interfaceOrientation的控制相当于全局的。
//每次试图切换的时候都会走的方法,用于控制设备的旋转方向.
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (_isRotation) {
return UIInterfaceOrientationMaskLandscape;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}


2.Controller层面的控制
//哪些页面支持自动转屏
- (BOOL)shouldAutorotate{

return YES;
}
// viewcontroller支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAllButUpsideDown;
 }

3.使得特定ViewController坚持特定的interfaceOrientation.
当然,使用这个方法是有前提的,就是当前ViewController是通过全屏的 Presentation方式展现出来的.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);


4.当前屏幕方向interfaceOrientation的获取.

 有3种方式可以获取到“当前interfaceOrientation”:
controller.interfaceOrientation,获取特定controller的方向
[[UIApplication sharedApplication] statusBarOrientation] 获取状态条相关的方向
[[UIDevice currentDevice] orientation] 获取当前设备的方向

你可能感兴趣的:(iOS屏幕旋转)