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

本文介绍了如何在iOS开发中,针对特定的ViewController实现横竖屏切换的功能。通过注册UIDevice的Orientation通知和在shouldAutorotateToInterfaceOrientation方法中返回YES,可以在不改变整体应用配置的情况下,使指定ViewController支持屏幕方向变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转: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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值