iPhone横竖屏转换

第一种:经过人为的办法改变view.transform的属性。app

具体办法:ide

    view.transform通常是View的旋转,拉伸移动等属性,相似view.layer.transform,区别在于 View.transform是二维的,也就是使用仿射的办法一般就是带有前缀CGAffineTransform的类(能够到API文档里面搜索这个前 缀的全部类),而view.layer.transform能够在3D模式下面的变化,一般使用的都是前缀为CATransform3D的类。布局

    这里要记住一点,当你改变过一个view.transform属性或者view.layer.transform的时候须要恢复默认状态的话,记得先把他 们重置能够使用view.transform = CGAffineTransformIdentity,或者view.layer.transform = CATransform3DIdentity,假设你一直不断的改变一个view.transform的属性,而每次改变以前没有重置的话,你会发现后来 的改变和你想要的发生变化了,不是你真正想要的结果。动画

    好了,上面介绍了旋转的属性,接下来就是关键了。官方提供了一个办法就是查看当前电池条的状态UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;经过这个办法,你能够知道当前屏幕的电池条的显示方向,并且你还能够 强制设置他的显示方向,经过设置这个属性就OK了,能够选择是否动画改变电池条方向。有了这两个那咱们就能够任意的改变咱们想要的显示方式了。spa

1.获取当前电池条的方向UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation.net

2.获取当前屏幕的大小CGRect frame = [UIScreen mainScreen].applicationFrame;code

3.设置咱们的View的中心点
    CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));orm

4.根据当前电池条的方向,获取须要旋转的角度的大小。一般对象

if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(M_PI*1.5);
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        return CGAffineTransformMakeRotation(M_PI/2);
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        return CGAffineTransformMakeRotation(-M_PI);
    } else {
        return CGAffineTransformIdentity;
    }blog

5.能够动画的改变咱们view的显示方式了
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];

CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;(获取当前电池条动画改变的时间)
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];

       //在这里设置view.transform须要匹配的旋转角度的大小就能够了。

        [UIView commitAnimations];

第二种:经过setOrientation:的办法强制性的旋转到一个特定的方向。

    注意:Apple在3.0之后都不支持这个办法了,这个办法已经成为了私有的了,可是要跳过App Stroe的审核,须要一点巧妙的办法。

    不要直接调用[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight]这样的办法来强制性的横屏,这样致使你的程序是很难经过App Store审核的。可是你能够选择使用performSelector的办法来调用它。具体就几行代码以下:

//强制横屏
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        [[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                                       withObject:(id)UIInterfaceOrientationLandscapeRight];
    }

    总结:若是第一种办法能够知足你须要的话,最好使用第一种办法,由于那个上 App Store确定没问问题,可是第二种的话是须要冒风险的,可是若是你的结构太复杂了,致使使用第一种办法人为很难控制的话,能够尝试简单的使用第二种办 法。我在有米提供的sample里面就看到他使用了第二种简单的办法来显示横屏竖式的广告条。

 

 

ipad横竖屏切换解决方案

  因为ipad的横竖屏不一样,因此好的应用,横竖屏的页面布局也不同。那么就须要横竖屏的总体解决方案。先看一个横竖屏布局不同的界面。

image image

上面两张图是来自同一个界面的横竖版的截屏。能够看出,横竖版显示的内容相同,可是界面布局不一样。要实现上述布局,主要是运用UIView中 layoutSubviews方法。当UIView设置为自动适配屏幕时,当用户旋转设备的时候,会调用layoutSubviews方法,咱们只需重写 这个方法,而后判断用户屏幕的方向。在调整每一个空间的位置便可。

 

下面是实现上述界面的最简单的原型:

  • 首先分析能够知道左面是图片,右面是一个图片加文字的视图。下面就实现一个左面视图右面是一个图加一段字的事例。

事例的截图以下:

image image

 

  • 其中右面的文字和绿色部分是用一个子视图封装的。
  • 整个布局是我在主视图中添加了一个ContentView视图,在ContentView视图中添加了一个ArticleView视图。
  • 其中ArticleView和ContentView的xib文件都打开了

image

  • 在ContentView中重写layoutSubviews方法,而后根据stausbar的方向判断当前视图的横竖屏。具体代码:

    -(void)layoutSubviews{ 
        [super layoutSubviews]; 
        UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation]; 
        if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) { 
            //翻转为竖屏时 
            [self setVerticalFrame]; 
        }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) { 
            //翻转为横屏时 
            [self setHorizontalFrame]; 
        } 
    }

    -(void)setVerticalFrame 

        NSLog(@"竖屏"); 
        [titleLable setFrame:CGRectMake(283, 0, 239, 83)]; 
        [leftView setFrame:CGRectMake(38, 102, 384, 272)]; 
        [rightView setFrame:CGRectMake(450, 102, 282, 198)]; 
    }

    -(void)setHorizontalFrame 

        NSLog(@"横屏"); 
        [titleLable setFrame:CGRectMake(183, 0, 239, 83)]; 
        [leftView setFrame:CGRectMake(168, 122, 384, 272)]; 
        [rightView setFrame:CGRectMake(650, 122, 282, 198)]; 
    }

    在具体的横竖屏方法中,重新设置各个组件的坐标便可。

  • 接下来在ContentView中添加ArticleView视图。

    -(id)initWithCoder:(NSCoder *)aDecoder 

        if ((self = [super initWithCoder:aDecoder])) {

            NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil]; 
            rightView=[arrayContentView objectAtIndex:0]; 
            [self addSubview:rightView]; 
        } 
        return self; 
    }

因为我用的是xib,因此初始化方法为initWithCoder,在这个中添加新的视图。

一样在ArticleView中设置横竖屏相应空间的坐标便可。


-(void)layoutSubviews{ 
    [super layoutSubviews]; 
    UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation]; 
    CGRect rect=self.frame; 
    rect.size.width=282; 
    rect.size.height=198; 
    [self setFrame:rect]; 
    if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) { 
        //翻转为竖屏时 
        [self setVerticalFrame]; 
    }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) { 
        //翻转为横屏时 
        [self setHorizontalFrame]; 
    } 
}

-(void)setVerticalFrame 

    NSLog(@"竖屏"); 
    [contentView setFrame:CGRectMake(12, 6, 250, 125)]; 
    [textLable setFrame:CGRectMake(50, 139, 182, 39)]; 
}

-(void)setHorizontalFrame 

    NSLog(@"横屏"); 
    [contentView setFrame:CGRectMake(12, 6, 106, 158)]; 
    [textLable setFrame:CGRectMake(135, 11, 147, 39)]; 
}

 

 

 

  1. </pre> 需求:程序默认开启是横屏,在压到某个页面的时候是竖屏<p></p><p>< /p><p>1.默认横屏须要在XX-info.plist 的 Supported interface orientations 的第一项为 Landscape (left home button)</p><p>2.在切换的竖屏的时候< /p><p></p><pre name="code" class="cpp">-(void)viewWillAppear:(BOOL)animated  
  2. {  
  3.     //[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];  
  4.     UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
  5.       
  6.     if (orientation != UIInterfaceOrientationPortrait ||   
  7.         orientation != UIInterfaceOrientationPortraitUpsideDown)  
  8.     {  
  9.         [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];  
  10.           
  11.         //(获取当前电池条动画改变的时间  
  12.         CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;  
  13.         [UIView beginAnimations:nil context:nil];  
  14.         [UIView setAnimationDuration:duration];  
  15.           
  16.         //在这里设置view.transform须要匹配的旋转角度的大小就能够了。  
  17.         self.view.transform = [self getTransformMakeRotationByOrientation:orientation];  
  18.         [UIView commitAnimations];  
  19.     }    
  20.       
  21.       
  22. }  

计算方向

  1. - (CGAffineTransform)getTransformMakeRotationByOrientation:(UIInterfaceOrientation)orientation  
  2. {  
  3.     if (orientation == UIInterfaceOrientationLandscapeLeft)   
  4.     {  
  5.         return CGAffineTransformMakeRotation(M_PI/2);  
  6.     }   
  7.     else if (orientation == UIInterfaceOrientationLandscapeRight)   
  8.     {  
  9.         return CGAffineTransformMakeRotation(M_PI/2);  
  10.     }   
  11.     else if (orientation == UIInterfaceOrientationPortraitUpsideDown)  
  12.     {  
  13.         return CGAffineTransformMakeRotation(-M_PI);  
  14.     }   
  15.     else   
  16.     {  
  17.           
  18.     }  
  19.       
  20.     return CGAffineTransformIdentity;  
  21.       
  22. }  


 

3.返回的到竖屏

 

  1. - (void)viewWillDisappear:(BOOL)animated  
  2. {  
  3.   
  4.       
  5.     UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
  6.       
  7.     if (orientation != UIInterfaceOrientationLandscapeLeft ||   
  8.         orientation != UIInterfaceOrientationLandscapeRight)  
  9.     {  
  10.         [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];  
  11.           
  12.         //(获取当前电池条动画改变的时间  
  13.         CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;  
  14.         [UIView beginAnimations:nil context:nil];  
  15.         [UIView setAnimationDuration:duration];  
  16.           
  17.         self.view.transform = CGAffineTransformIdentity;  
  18.         [UIView commitAnimations];  
  19.     }    
  20. }  

 

 

iPad上的应用通常都会横竖屏支持,因此同一个界面页面的横竖布局也是不同的。要实现横竖布局的不同通常用到了UIView的layoutSubView方法。当UIView设置为自动适配屏幕时,当用户设备旋转的时候,就会调用layoutSubView这个方法,只要重写这个方法,而后判断屏幕的方向,调整控件的位址就能够了。如今你们可能会有些疑问,为何不在UIViewController的这个方法:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
进行判断。由于这个方法是做用于UIViewController下的,而不是直接做用于UIView进行控制,因此会发生延迟,做用的对象也不正确。因此建议用layoutSubView这个方法
 
使用列子:
 

-(void)layoutSubviews{ 

    [super layoutSubviews]; 

    UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication]statusBarOrientation]; 

    if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation ==UIDeviceOrientationPortraitUpsideDown) { 

        //翻转为竖屏时 

        [self setVerticalFrame]; 

    }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation ==UIDeviceOrientationLandscapeRight) { 

        //翻转为横屏时 

        [self setHorizontalFrame]; 

    } 

}

-(void)setVerticalFrame 

    NSLog(@"竖屏"); 

    [titleLable setFrame:CGRectMake(283023983)]; 

    [leftView setFrame:CGRectMake(38102384272)]; 

    [rightView setFrame:CGRectMake(450102282198)]; 

}

-(void)setHorizontalFrame 

    NSLog(@"横屏"); 

    [titleLable setFrame:CGRectMake(183023983)]; 

    [leftView setFrame:CGRectMake(168122384272)]; 

    [rightView setFrame:CGRectMake(650122282198)]; 

}