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

首先,我们必须了解一下下列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. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  12. {
  13. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  14. }
  15. -(void)rotation_btn:(float)n
  16. {
  17. UIButton *robtn = self.btn;
  18. robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
  19. }
  20. -(void)orientationChanged
  21. {
  22. UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];
  23. switch (orientaiton) {
  24. caseUIDeviceOrientationPortrait:
  25. [self  rotation_btn:0.0];
  26. break;
  27. caseUIDeviceOrientationPortraitUpsideDown:
  28. [self  rotation_btn:90.0*2];
  29. break;
  30. caseUIDeviceOrientationLandscapeLeft:
  31. [self  rotation_btn:90.0*3];
  32. break;
  33. caseUIDeviceOrientationLandscapeRight:
  34. [self  rotation_btn:90.0];
  35. break;
  36. default:
  37. break;
  38. }
  39. }

也许,你需要autoresizesSubviews = YES

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

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

来帮助我们完成自己想要的适应效果。Example 动画呈现的很清晰,^_^ 我就不再啰嗦了。

iOS旋屏的更多相关文章

  1. iOS 旋屏问题

    今天突然想起来,以前的一个问题没有解决,就上网百度了一些方法,看到一篇文章,写的很详细,我就操作试试,结果还真的实现了功能,接下来我将重复他的结合我自己的测试,说一下iOS中的旋屏问题. 1.首先配置 ...

  2. ios 导航栏和旋屏

    1,状态栏(UIStatusBar) http://my.oschina.net/shede333/blog/304560 2,visibleViewController和topViewControl ...

  3. iOS特殊界面旋屏设置的方法之一

    1.AppDelegate.h @property (assign, nonatomic) BOOL allowRotation; 2.AppDelegate.m #pragma mark - 自动旋 ...

  4. iOS6的旋屏控制技巧

    在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如: ...

  5. UIImagePickerController在UIPopoverController中 旋屏问题

    1弧度=180/π度1度=π/180弧度今天遇到了 一个问题.UIImagePickerController在UIPopoverController中 旋屏问题. 在查找了许多资料后方知,此乃iOS系 ...

  6. iOS离屏渲染之优化分析

    在进行iOS的应用开发过程中,有时候会出现卡顿的问题,虽然iOS设备的性能越来越高,但是卡顿的问题还是有可能会出现,而离屏渲染是造成卡顿的原因之一.因此,本文主要分析一下离屏渲染产生的原因及避免的方法 ...

  7. Yosemite 给 iOS 录屏

    [Yosemite 给 iOS 录屏] Mac 升级到Yosemite后,支持iOS屏幕录制.把Mac和iPhone用数据线相连.打开QuickTime Player,新建一个影片. 从摄像头源中选择 ...

  8. 同时支持Android 和 ios 投屏到电脑的软件,Support Android and ios screen shrare to PC - 希沃授课助手

    最近学校由粉笔黑板更换了智慧电子黑板,然后发现了一个好玩的软件. 感谢希沃公司的开发: 希沃授课助手,这是一款同时支持Android 和 ios 投屏和远程控制的. 效果很流畅,非常赞

  9. iOS 离屏渲染的研究

    GPU渲染机制: CPU 计算好显示内容提交到 GPU,GPU 渲染完成后将渲染结果放入帧缓冲区,随后视频控制器会按照 VSync 信号逐行读取帧缓冲区的数据,经过可能的数模转换传递给显示器显示. G ...

随机推荐

  1. UGUI DOTween渐隐渐现

    Tween tweenAlpha; tweenAlpha = DOTween.To(() => MaskSpr.fillAmount, x => MaskSpr.fillAmount = ...

  2. Elasticsearch学习记录(分布式的特性)

    Elasticsearch学习记录(分布式的特性) 分布式的特性 我们提到Elasticsearch可以扩展到上百(甚至上千)的服务器来处理PB级的数据.然而我们的例子只给出了一些使用Elastics ...

  3. PJzhang:互联网是有国界

    猫宁!!! 参考链接:https://mp.weixin.qq.com/s/NFgps_5HBpl3ZjDoR5w8XA 上世纪90年代,互联网开始崛起,美国凭借超级大国的地位,先进技术优势,以美国公 ...

  4. mysql ERROR 2003 (HY000): Can't connect to MySQL server on '' (10060

    关闭防火墙即可连接成功: systemctl stop firewalld

  5. python多线程Event实现红绿灯案例

    代码: # __author__ = 'STEVEN' # coding = utf-8 import time,threading #开启事件 event = threading.Event() c ...

  6. JVM-GC日志分析

    程序运行时配置如下参数: -Xms20M -Xmx20M -Xmn10M -verbose:gc -XX:+PrintGCDetails -XX:SurvivorRatio= -XX:+PrintGC ...

  7. matlab各向异性扩散滤波

    主要是用来平滑图像的,克服了高斯模糊的缺陷,各向异性扩散在平滑图像时是保留图像边缘的(和双边滤波很像). 通常我们有将图像看作矩阵的,看作图的,看作随机过程的,记得过去还有看作力场的. 这次新鲜,将图 ...

  8. gulp-htmlone的BUG弃坑

    之前用项目用gulp-htmlone做最后的js和css内联打包出现各种问题 这次居然遇到打包后的css斜杠变反斜杠的问题 如下:/src/common/images/i_banner.jpg会被改成 ...

  9. WCF中的AsyncPattern

    WCF中的AsyncPattern   (系列博文源自 http://pfelix.wordpress.com/,由笔者翻译并整理,转载请注明) 在wcf 的 service contract中, 服 ...

  10. 极简的css浮动窗口

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...