zoukankan      html  css  js  c++  java
  • 屏幕自动旋转和调节大小

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

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

    1. //  RotateViewController.h  
    2. #import <UIKit/UIKit.h>  
    3.   
    4. @interface RotateViewController : UIViewController  
    5. {  
    6.     UILabel *upLabel;  
    7.     UILabel *downLabel;  
    8.     CGRect upFrame;  
    9.     CGRect dowmFrame;  
    10. }  
    11. @end  
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.     // Do any additional setup after loading the view, typically from a nib.  
    5.     upFrame = CGRectMake(10, 10, 300, 100);  
    6.     dowmFrame = CGRectMake(10, 350, 300, 100);  
    7. //    初始化Label  
    8.     upLabel = [[UILabel alloc] initWithFrame:upFrame];  
    9.     downLabel = [[UILabel alloc] initWithFrame:dowmFrame];  
    10. //    设置标题  
    11.     [upLabel setText:@"在上面"];  
    12.     [downLabel setText:@"在下面"];  
    13. //    设置字体大小  
    14.     upLabel.font = [UIFont systemFontOfSize:48.0];  
    15.     downLabel.font = [UIFont systemFontOfSize:48.0];  
    16. //    字体加粗  
    17.     upLabel.font = [UIFont boldSystemFontOfSize:48];  
    18.     downLabel.font = [UIFont boldSystemFontOfSize:48];  
    19.       
    20. //    如果label标题很长,可以使用此方法让标题大小自动适合label宽度  
    21.     /* 
    22.     upLabel.adjustsFontSizeToFitWidth =YES; 
    23.     downLabel.adjustsFontSizeToFitWidth = YES; 
    24.     */  
    25.       
    26. //    设置Label标题居中对其  
    27.     upLabel.textAlignment=UITextAlignmentCenter;  
    28.     downLabel.textAlignment = UITextAlignmentCenter;  
    29. //    设置label标题颜色  
    30.     [upLabel setTextColor:[UIColor blackColor]];  
    31.     [downLabel setTextColor:[UIColor blueColor]];  
    32.       
    33. //    设置Label透明度0是完全透明,1是不透明  
    34.     upLabel.alpha = 0.9;  
    35.     downLabel.alpha = 0.4;  
    36. //    把label添加到视图上      
    37.     [self.view addSubview:upLabel];  
    38.     [self.view addSubview:downLabel];  
    39. }  

    ⌘「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//Home键在上  
    3. UIInterfaceOrientationLandscapeLeft//Home键在左  
    4. UIInterfaceOrientationLandscapeRight//Home键在右  

    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.     //更改视图视图属性,设置为YES  
    5.     backgroundView.userInteractionEnabled=YES;  
    6.       
    7.     //添加至主视图,两个方法实现功能一样  
    8.    [self setView:backgroundView];     
    9. //   [self.view addSubview:backgroundView];  


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

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

    1. //  RotateViewController.h  
    2. #import <UIKit/UIKit.h>  
    3.   
    4. //宏定义,把度数转化成弧度  
    5. #define degreesToRadians(x) (M_PI * (x) / 180.0)  
    6.   
    7. @interface RotateViewController : UIViewController  
    8. {  
    9.     UILabel *upLabel;  
    10.     UILabel *downLabel;  
    11.     CGRect upFrame;  
    12.     CGRect dowmFrame;  
    13.       
    14.     UIImageView *backgroundView;  
    15.       
    16.     UIView *landscape;  
    17.     UIView *portrait;  
    18.   
    19. }  
    20.   
    21.   
    22. @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.     //更改视图视图属性,设置为YES  
    17.     backgroundView.userInteractionEnabled=YES;  
    18.       
    19.       
    20.   
    21.     upFrame = CGRectMake(10, 10, 300, 100);  
    22.     dowmFrame = CGRectMake(10, 350, 300, 100);  
    23.     //    初始化Label  
    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.     //    如果label标题很长,可以使用此方法让标题大小自动适合label宽度  
    37.       
    38.     //    upLabel.adjustsFontSizeToFitWidth =YES;  
    39.     //    downLabel.adjustsFontSizeToFitWidth = YES;  
    40.       
    41.       
    42.     //    设置Label标题居中对其  
    43.     upLabel.textAlignment=UITextAlignmentCenter;  
    44.     downLabel.textAlignment = UITextAlignmentCenter;  
    45.     //    设置label标题颜色  
    46.     [upLabel setTextColor:[UIColor blackColor]];  
    47.     [downLabel setTextColor:[UIColor blueColor]];  
    48.       
    49.     //    设置Label透明度0是完全透明,1是不透明  
    50.     upLabel.alpha = 0.9;  
    51.     downLabel.alpha = 0.4;  
    52.          
    53.   
    54.       
    55.     //    把label添加到视图上    
    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 (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
    5.         upLabel.frame = upFrame; 
    6.         downLabel.frame = dowmFrame; 
    7.     } 
    8.     else { 
    9.         upLabel.frame = CGRectMake(90, 10, 300, 100); 
    10.         downLabel.frame = CGRectMake(90, 190, 300, 100); 
    11.     } 
    12.      *****************************************/  
    13.     if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {  
    14.           
    15.           
    16.         self.view = portrait;  
    17.   
    18.         upLabel.frame = upFrame;  
    19.         downLabel.frame = dowmFrame;  
    20.         [self setView:backgroundView];   
    21.         [self.view addSubview:upLabel];  
    22.         [self.view addSubview:downLabel];          
    23.         self.view.transform = CGAffineTransformIdentity;  
    24.         self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));  
    25.         self.view.bounds = CGRectMake(0, 0, 320, 460);  
    26.         NSLog(@"----------->down");  
    27.     }  
    28.     else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {  
    29.          
    30.         self.view = landscape;  
    31.   
    32.         upLabel.frame = CGRectMake(90, 10, 300, 100);  
    33.         downLabel.frame = CGRectMake(90, 190, 300, 100);  
    34.           
    35.         [self setView:backgroundView];   
    36.         [self.view addSubview:upLabel];  
    37.         [self.view addSubview:downLabel];  
    38.   
    39. //        需要修改transfom属性,此处是回复默认状态,每次变换前都要置位,不然你变换用的坐标系统不是屏幕坐标系统(即绝对坐标系统),而是上一次变换后的坐标系统          
    40.         self.view.transform = CGAffineTransformIdentity;  
    41. //        获取旋转弧度数  
    42.         self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));  
    43.         self.view.bounds = CGRectMake(0, 0, 480, 300);  
    44.         NSLog(@"----------->Left");  
    45.     }  
    46.     else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {  
    47.                 self.view = portrait;  
    48.           
    49.   
    50.         upLabel.frame = upFrame;  
    51.         downLabel.frame = dowmFrame;  
    52.         [self setView:backgroundView];   
    53.         [self.view addSubview:upLabel];  
    54.         [self.view addSubview:downLabel];  
    55.           
    56.         self.view.transform = CGAffineTransformIdentity;  
    57.         self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));  
    58.         self.view.bounds = CGRectMake(0, 0,320 , 460);  
    59.         NSLog(@"----------->up");  
    60.     }  
    61.     else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {  
    62.           
    63.         self.view = landscape;  
    64.           
    65.         upLabel.frame = CGRectMake(90, 10, 300, 100);  
    66.         downLabel.frame = CGRectMake(90, 190, 300, 100);  
    67.         [self setView:backgroundView];   
    68.         [self.view addSubview:upLabel];  
    69.         [self.view addSubview:downLabel];  
    70.   
    71.         self.view.transform = CGAffineTransformIdentity;  
    72.         self.view.transform  = CGAffineTransformMakeRotation(degreesToRadians(90));  
    73.         self.view.bounds = CGRectMake(0, 0, 480, 300);  
    74.           
    75.           
    76.         NSLog(@"----------->right");  
    77.     }  
    78. }  

    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  

    Blog都是随笔,只当做笔记,不会有详细介绍,测试请慎重。。。
  • 相关阅读:
    [Json.net]忽略不需要的字段
    [c#]exchange回复,全部回复,转发所遇到的问题
    [c#]获取exchange中的图片
    [c#基础]AutoResetEvent
    [C#基础]c#中的BeginInvoke和EndEndInvoke
    [CentOs7]安装mysql
    [CentOS7]安装mysql遇到的问题
    [CentOs7]图形界面
    [CentOS]添加删除用户
    在虚机中安装CentOS
  • 原文地址:https://www.cnblogs.com/JerryBaxia/p/4985670.html
Copyright © 2011-2022 走看看