iPhone开发应用中如何以消息通知方式设置旋转View

来源:互联网 发布:python快速入门 编辑:程序博客网 时间:2024/06/06 01:35

转:http://mobile.51cto.com/iphone-285285.htm

iPhone开发应用中以消息通知方式设置旋转View是本文要介绍的内容,主要是来学如何来旋转View,具体内容来看本文详解。看到这篇不错, 直接转载过来了, 没什么可解释的,一看就明白 :)。

整个程序需要支持横竖屏切换得时候,会比较简单,在每个ViewController 的

  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

方法中,return YES; 就好。

可如果只是要某个VC( = View Controller)支持横竖屏切换呢?单独在那个view controller中像上面那样做是没有效果的。

这个时候我们可以取 UIDevice的 Orientation来判定:

1、在VC中注册 UIDevice 的 UIDeviceOrientationDidChangeNotification 通知:

  1. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];  
  2. [center addObserver:self selector:@selector(doRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

2、在自己的 doRotate函数里面处理:

  1. - (void)doRotate:(NSNotification *)notification{  
  2. UIDevice *myDevice = [UIDevice currentDevice];  
  3. UIDeviceOrientation deviceOrientation = [myDevice orientation];  
  4. UIApplication *app = [UIApplication sharedApplication];  
  5. [app setStatusBarOrientation:deviceOrientation];  

3.

  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

也是要 return YES。

原创粉丝点击