惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 张永存(Jerry)

POD 创建 Xcode 项目组 ReactiveCocoa RACObserve subscribeNext 时,只有值不一样时才响应 ReactiveCocoa 监听Enabled和添加Command出错的处理方法 Masonry + UIView Animations 注意事项 addObserver forKeyPath options 注意事项 ios中tabbar得title和navigationbar的title如何修改 tableview 分组显示返回footerviewt和headerView的高度不能为0的问题 UITableViewCell的选中时的颜色设置 ios 枚举 位移操作 设置UIButton 字体 颜色 ios NSString format 保留小数点 float double IOS影响Animiation动画的事件 类似9patch效果的iOS图片拉伸 CocoaPods安装 Mac OS X上安装 Ruby运行环境 XMPP 连续注册出现用户限制:500错误 处理方式 ejabberd 搭配的XMPP服务器注册失败 403 处理 在framework中打包xib 【转】 XMPP ejabberd服务 Mac 安装
屏幕自动旋转和调节大小
张永存(Jerry) · 2015-11-22 · via 博客园 - 张永存(Jerry)

1.新建工程名为RotateDemo , File->New->Project ->single View Application -> next

2.在view视图上添加两个Label,

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface RotateViewController : UIViewController  
  4. {  
  5.     UILabel *upLabel;  
  6.     UILabel *downLabel;  
  7.     CGRect upFrame;  
  8.     CGRect dowmFrame;  
  9. }  
  10. @end  
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     
  5.     upFrame = CGRectMake(10, 10, 300, 100);  
  6.     dowmFrame = CGRectMake(10, 350, 300, 100);  
  7.     upLabel = [[UILabel alloc] initWithFrame:upFrame];  
  8.     downLabel = [[UILabel alloc] initWithFrame:dowmFrame];  
  9.     [upLabel setText:@"在上面"];  
  10.     [downLabel setText:@"在下面"];  
  11.     upLabel.font = [UIFont systemFontOfSize:48.0];  
  12.     downLabel.font = [UIFont systemFontOfSize:48.0];  
  13.     upLabel.font = [UIFont boldSystemFontOfSize:48];  
  14.     downLabel.font = [UIFont boldSystemFontOfSize:48];  
  15.       
  16.     
  17.       
  18.     upLabel.textAlignment=UITextAlignmentCenter;  
  19.     downLabel.textAlignment = UITextAlignmentCenter;  
  20.     [upLabel setTextColor:[UIColor blackColor]];  
  21.     [downLabel setTextColor:[UIColor blueColor]];  
  22.       
  23.     upLabel.alpha = 0.9;  
  24.     downLabel.alpha = 0.4;  
  25.     [self.view addSubview:upLabel];  
  26.     [self.view addSubview:downLabel];  
  27. }  

⌘「Command 键」+ ->  可以切换视图旋转方向,或者菜单栏点击硬件,在下拉菜单里有旋转方向控制选项,运行结果截图
 

        出现这情况是iphone根据重力感应所产生的,view视图发生改变,但是两个label坐标并未改变,所以在下面那个label并未显示出来,还有个 就是当Home方向向上的时候,和第二中情况一样了,label坐标发生变化了,这是针对iphone的特殊情况,当有电话呼入的时候,如果Home键在 上,那么我们的话筒和听筒就反了,主要调用的是这个实例方法

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

他的意思就是不支持Home键在上方,如果是ipad我们只需return YES;意思支持屏幕任何方向的旋转,

ios设置了四个方向变量

  1. UIInterfaceOrientationPortrait
  2. UIInterfaceOrientationPortraitUpsideDown
  3. UIInterfaceOrientationLandscapeLeft
  4. UIInterfaceOrientationLandscapeRight

3.重写willAnimateRotationToInterfaceOrientation方法,重新设置控件的大小与位置

  1. - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration  
  2. {  
  3.     if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {  
  4.         upLabel.frame = upFrame;  
  5.         downLabel.frame = dowmFrame;  
  6.     }  
  7.     else {  
  8.         upLabel.frame = CGRectMake(90, 10, 300, 100);  
  9.         downLabel.frame = CGRectMake(90, 190, 300, 100);  
  10.     }  
  11. }  

       当屏幕出现旋转开始并在动画发生之前自动调用此方法,首先if判断屏幕方向是竖着(UIInterfaceOrientationIsPortrait)的还是横着(UIInterfaceOrientationIsLandscape),toInterfaceOrientation是传入参数,从变量命名意思我们就可以看出是界面方向,此方法是对label进行屏幕不同方向的重新布局,

处理屏幕旋转问题的函数还有几个,他们之间有的放在一起会相互制约,而且在模拟器下只有

  1. willAnimateRotationToInterfaceOrientation    <span style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; text-align: left; text-indent: 28px;font-size:16px; ">willRotateToInterfaceOrientation     <span style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; text-align: left; text-indent: 28px;font-size:16px; ">didRotateFromInterfaceOrientation</span></span>  

三个方法可以使用,其他的只能在真机上使用:此处参考的是http://blog.csdn.net/sjzsp/article/details/6364585

//在视图旋转动画前一半发生之前自动调用
-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

{


}

//在视图旋转动画后一半发生之前自动调用
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration

{

}

//该方法将在视图旋转之前自动调用

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

{


}

//该方法在试图旋转完成之后调用
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{


}

//在试图旋转动画前一半发生之前自动调用
-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{


}

4.若在在视图中添加一张背景图片,图片会不会自动旋转调节大小呢,拖动一张background.png图片到工程中,在ViewDidLoad上添加一下代码,并运行下结果:

  1. UIImage *image = [UIImage imageNamed:@"background.png"];  
  2.     
  3.     UIImageView *backgroundView = [[UIImageView alloc] initWithImage:image];  
  4.     
  5.     backgroundView.userInteractionEnabled=YES;  
  6.       
  7.     
  8.    [self setView:backgroundView];     


从中我们可以看出,图片是可以根据视图自动调节大小的,但是我想提的一点[self.view addSubview:backgroundView];测试用的是[self setView:backgroundView];他们功能是一样的,不要放在两个[self.View addSubView:upLabel];h和[self.view addSubView:downLabel]后面,否则就会把两个Label给覆盖了,两个label是在backgroundView上没的;

5.对于比较复杂的界面上一种方法就不是那么好控制,如何去实现视图的横向和纵向视图的分离呢,分别设计视图横向模式和纵向模式,然后在实现他们之间的切换操作,代码如下

  1. #import <UIKit/UIKit.h>  
  2.   
  3. #define degreesToRadians(x) (M_PI * (x) / 180.0)  
  4.   
  5. @interface RotateViewController : UIViewController  
  6. {  
  7.     UILabel *upLabel;  
  8.     UILabel *downLabel;  
  9.     CGRect upFrame;  
  10.     CGRect dowmFrame;  
  11.       
  12.     UIImageView *backgroundView;  
  13.       
  14.     UIView *landscape;  
  15.     UIView *portrait;  
  16.   
  17. }  
  18.   
  19.   
  20. @end  


viewDidLoad方法

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.       
  6.     portrait = [[UIView alloc] initWithFrame:self.view.frame];  
  7.       
  8.     landscape = [[UIView alloc]initWithFrame:CGRectMake(0, 0,480, 30)];  
  9.       
  10.       
  11.       
  12.       
  13.     UIImage *image = [UIImage imageNamed:@"background.png"];  
  14.     
  15.    backgroundView = [[UIImageView alloc] initWithImage:image];  
  16.     
  17.     backgroundView.userInteractionEnabled=YES;  
  18.       
  19.       
  20.   
  21.     upFrame = CGRectMake(10, 10, 300, 100);  
  22.     dowmFrame = CGRectMake(10, 350, 300, 100);  
  23.     
  24.     upLabel = [[UILabel alloc] initWithFrame:upFrame];  
  25.     downLabel = [[UILabel alloc] initWithFrame:dowmFrame];  
  26.     
  27.     [upLabel setText:@"在上面"];  
  28.     [downLabel setText:@"在下面"];  
  29.     
  30.     upLabel.font = [UIFont systemFontOfSize:48.0];  
  31.     downLabel.font = [UIFont systemFontOfSize:48.0];  
  32.     
  33.     upLabel.font = [UIFont boldSystemFontOfSize:48];  
  34.     downLabel.font = [UIFont boldSystemFontOfSize:48];  
  35.       
  36.     
  37.       
  38.     
  39.     
  40.       
  41.       
  42.     
  43.     upLabel.textAlignment=UITextAlignmentCenter;  
  44.     downLabel.textAlignment = UITextAlignmentCenter;  
  45.     
  46.     [upLabel setTextColor:[UIColor blackColor]];  
  47.     [downLabel setTextColor:[UIColor blueColor]];  
  48.       
  49.     
  50.     upLabel.alpha = 0.9;  
  51.     downLabel.alpha = 0.4;  
  52.          
  53.   
  54.       
  55.     
  56.     [self.view addSubview:backgroundView];  
  57.     [self.view addSubview:upLabel];  
  58.     [self.view addSubview:downLabel];  
  59.             
  60.       
  61. }  


为了支持任何方向的旋转,shouldAutorotateToInterfaceOrientation,方法的返回值设置成return YES; 然后继续重写willAnimateRotationToInterfaceOrientation方法

  1. - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration  
  2. {  
  3.     
  4.     if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {  
  5.           
  6.           
  7.         self.view = portrait;  
  8.   
  9.         upLabel.frame = upFrame;  
  10.         downLabel.frame = dowmFrame;  
  11.         [self setView:backgroundView];   
  12.         [self.view addSubview:upLabel];  
  13.         [self.view addSubview:downLabel];          
  14.         self.view.transform = CGAffineTransformIdentity;  
  15.         self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));  
  16.         self.view.bounds = CGRectMake(0, 0, 320, 460);  
  17.         NSLog(@"----------->down");  
  18.     }  
  19.     else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {  
  20.          
  21.         self.view = landscape;  
  22.   
  23.         upLabel.frame = CGRectMake(90, 10, 300, 100);  
  24.         downLabel.frame = CGRectMake(90, 190, 300, 100);  
  25.           
  26.         [self setView:backgroundView];   
  27.         [self.view addSubview:upLabel];  
  28.         [self.view addSubview:downLabel];  
  29.   
  30.         self.view.transform = CGAffineTransformIdentity;  
  31.         self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));  
  32.         self.view.bounds = CGRectMake(0, 0, 480, 300);  
  33.         NSLog(@"----------->Left");  
  34.     }  
  35.     else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {  
  36.                 self.view = portrait;  
  37.           
  38.   
  39.         upLabel.frame = upFrame;  
  40.         downLabel.frame = dowmFrame;  
  41.         [self setView:backgroundView];   
  42.         [self.view addSubview:upLabel];  
  43.         [self.view addSubview:downLabel];  
  44.           
  45.         self.view.transform = CGAffineTransformIdentity;  
  46.         self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));  
  47.         self.view.bounds = CGRectMake(0, 0,320 , 460);  
  48.         NSLog(@"----------->up");  
  49.     }  
  50.     else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {  
  51.           
  52.         self.view = landscape;  
  53.           
  54.         upLabel.frame = CGRectMake(90, 10, 300, 100);  
  55.         downLabel.frame = CGRectMake(90, 190, 300, 100);  
  56.         [self setView:backgroundView];   
  57.         [self.view addSubview:upLabel];  
  58.         [self.view addSubview:downLabel];  
  59.   
  60.         self.view.transform = CGAffineTransformIdentity;  
  61.         self.view.transform  = CGAffineTransformMakeRotation(degreesToRadians(90));  
  62.         self.view.bounds = CGRectMake(0, 0, 480, 300);  
  63.           
  64.           
  65.         NSLog(@"----------->right");  
  66.     }  
  67. }  

self.view.transform = CGAffineTransformIdentity;

self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));

两次修改transform,原因是第一次需要修改transfom属性此处是回复默认状态,每次变换前都要置位,不然你变换用的坐标系统不是屏幕坐标系统(即绝对坐标系统),而是上一次变换后的坐标系统  ,第二次是设置旋转的弧度数,本人再次犯了一个很大低级错误,纠结了两个多小时,和书上一样的代码为什么在Home在上方和Home在下方是屏幕旋转方向不对,这两个方向上视图总是斜着的,郁闷死了,最后才发现是因为CGAffineTransformMakeRotation传参的时候,直接把角度数180和90传进去了,未作弧度装换,你可不要还这样低级错误哦;

最后再把运行截图贴上

  

6.还可以使用IB工具,使用Size Inpector实现自动旋转 也可以实现屏幕旋转和大小调节,需要使用控件,在此就不做测试, 附上源代码:http://download.csdn.net/detail/duxinfeng2010/4404812