捕获屏幕旋转
1.注册系统通知:(适用于所有页面)
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
此方法监听的是状态栏的变化,所以系统自动旋转被锁定的时候无法监听的到,常用此方法与系统设置保持一致,还可以监听UIDeviceOrientationDidChangeNotification这个通知,当系统自动旋转呗锁定时依然可以执行,当我们关注的只是物理朝向时,我们通常需要注册该通知来解决问题。
屏幕的旋转朝向可以通过 [[UIDevice currentDevice]orientation] 判断:
- (void)changeRotate:(NSNotification*)notify{
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
|| [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
//竖屏
} else {
//横屏
}
}
2.willAnimateRotationToInterfaceOrientation:适用于UIViewController,在你的UIViewController中重写该方法:
-( void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
//竖屏
}
else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
//横屏
}
}
手动调用系统旋转屏幕方法:
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
上面是旋转到竖屏,可以用UIDeviceOrientationLandscapeLeft或者UIDeviceOrientationLandscapeRight旋转到横屏。