旋屏

横竖屏切换,视图乱了怎么办?

首先,我们必须了解一下下列4种状态,它们被用来描述设备旋转方向:

UIInterfaceOrientationLandscapeLeft

向左,即HOME键在右

UIInterfaceOrientationLandscapeRight

向右,即HOME键在左

UIInterfaceOrientationPortrait

正立,即HOME键在下

UIInterfaceOrientationPortraitUpsideDown

倒立,即HOME键在上


 

对于旋屏的处理,大致分为如下几种情况和思路:

也许,你不需要旋屏支持,而希望锁定屏幕

  1. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.     return NO;  
  4. }  


也许,你需要支持旋屏,或者支持部分方向的旋屏

  1. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {   
  2.        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  3. }  


也许,你的view有张背景图,旋屏时系统帮助你拉伸了图片,但是却没有管你的其它部件,比如button,你希望直接改变button的大小和位置

  1. -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration  
  2. {  
  3.     if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {  
  4.         NSLog(@"现在是竖屏");  
  5.         [btn setFrame:CGRectMake(213, 442, 340, 46)];  
  6.     }  
  7.     if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {  
  8.         NSLog(@"现在是横屏");  
  9.         [btn setFrame:CGRectMake(280, 322, 460, 35)];  
  10.     }  
  11. }  


也许,你并不希望用绝对坐标去约束控件,而是希望让它通过旋转自己适应屏幕的旋转

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.     UIDevice *device = [UIDevice currentDevice];   
  6.     [device beginGeneratingDeviceOrientationNotifications];  
  7.     //利用 NSNotificationCenter 获得旋转信号 UIDeviceOrientationDidChangeNotification  
  8.     NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];   
  9.     [ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device];  
  10. }  
  11.   
  12. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  13. {  
  14.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  15. }  
  16.   
  17. -(void)rotation_btn:(float)n  
  18. {  
  19.     UIButton *robtn = self.btn;  
  20.     robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  21. }  
  22.   
  23. -(void)orientationChanged  
  24. {  
  25.     UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];  
  26.       
  27.     switch (orientaiton) {  
  28.         caseUIDeviceOrientationPortrait:               
  29.             [self  rotation_btn:0.0];  
  30.             break;  
  31.         caseUIDeviceOrientationPortraitUpsideDown:    
  32.             [self  rotation_btn:90.0*2];  
  33.             break;  
  34.         caseUIDeviceOrientationLandscapeLeft:       
  35.             [self  rotation_btn:90.0*3];  
  36.             break;  
  37.         caseUIDeviceOrientationLandscapeRight:    
  38.             [self  rotation_btn:90.0];  
  39.             break;  
  40.         default:  
  41.             break;  
  42.     }  
  43. }  


也许,你需要autoresizesSubviews = YES

也许,你希望横竖屏有不同的布局效果,需要准备2份Subview,在不同状态去替换


 

当然不要忘记,需要调节设定图示中的1、2处

来帮助我们完成自己想要的适应效果。Example 动画呈现的很清晰,^_^ 我就不再啰嗦了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值