IOS疯狂基础之屏幕旋转控制,获得当前方向
2013-03-14 17:57
453 查看
获得当前屏幕方向
self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation]
if (self.interfaceOrientation==UIDeviceOrientationLandscapeRight)
{
XXOO
}
不旋转,保持竖屏
始终保持横屏
在使用UINavigationController时发现,无论怎么设置上面方法的返回,都无法控制UINavigationController的旋转,这似乎是iOS的一个bug。但无论如何,以下是stackflow上面的一个解决方法:
屏幕旋转方法调用流程
要翻转的时候,首先响应的方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES则支持翻转,NO则不支持。
紧接着
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
再来是
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
最后调用的是
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放
self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation]
if (self.interfaceOrientation==UIDeviceOrientationLandscapeRight)
{
XXOO
}
不旋转,保持竖屏
//iOS 5 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait); } //iOS 6 - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }
始终保持横屏
//iOS 5 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == self.preferredInterfaceOrientationForPresentation); } //iOS 6 - (BOOL) shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight; }
在使用UINavigationController时发现,无论怎么设置上面方法的返回,都无法控制UINavigationController的旋转,这似乎是iOS的一个bug。但无论如何,以下是stackflow上面的一个解决方法:
1 @implementation UINavigationController (Rotation_IOS6) 2 -(BOOL)shouldAutorotate { 3 return [[self.viewControllers lastObject] shouldAutorotate]; 4 } 5 6 -(NSUInteger)supportedInterfaceOrientations { 7 return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 8 } 9 10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 11 return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 12 } 13 @end
屏幕旋转方法调用流程
要翻转的时候,首先响应的方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES则支持翻转,NO则不支持。
紧接着
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
再来是
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
最后调用的是
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放
相关文章推荐
- IOS疯狂基础之屏幕旋转控制,获得当前方向(转)
- IOS疯狂基础之屏幕旋转控制,获得当前方向
- IOS疯狂基础之屏幕旋转控制,获得当前方向
- IOS疯狂基础之UIView
- IOS 疯狂基础之 页面间跳转
- iOS 获得当前系统语言
- iOS-从任何一个view中获得当前控制器
- IOS 控制当前控制器支持哪些方向
- IPad iOS开发基础——ipad设备方向识别
- IOS疯狂基础之NSUserDefaults
- IOS疯狂基础之UINavigationController
- IOS疯狂基础之UILabel
- IOS疯狂基础之模态显示PresentModalViewController(转抄)
- iOS 百度地图当前位置方向问题解决方案
- 获得当前的屏幕方向
- 获得当前运行的设备上的 ios版本号
- IOS疯狂基础之NSNotificationCenter消息注册与撤销
- IOS疯狂基础之网络检测
- iOS 获得当前经纬度和城市
- iOS 获得当前页面正在显示的页面是哪一个Viewcontroller