服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS开发中控制屏幕旋转的编写方法小结

iOS开发中控制屏幕旋转的编写方法小结

2020-12-26 16:45念茜 IOS

这篇文章主要介绍了iOS开发中控制屏幕旋转的编写方法小结,包括横竖屏切换时视图所出现的问题等经常需要注意的地方,需要的朋友可以参考下

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

复制代码 代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

 

但是在iOS6中,这个方法被废弃了,使用无效。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)

实践后会发现,通过supportedInterfaceOrientations的单独控制是无法锁定屏幕的。

复制代码 代码如下:

-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskPortrait; 

 

多次实验后总结出控制屏幕旋转支持方向的方法如下:
子类化UINavigationController,增加方法

复制代码 代码如下:

- (BOOL)shouldAutorotate 

    return self.topViewController.shouldAutorotate; 

 
- (NSUInteger)supportedInterfaceOrientations 

    return self.topViewController.supportedInterfaceOrientations; 


并且设定其为程序入口,或指定为 self.window.rootViewController
随后添加自己的view controller,如果想禁止某个view controller的旋屏:(支持全部版本的控制)

复制代码 代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

 
-(BOOL)shouldAutorotate 

    return NO; 

 
-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskPortrait; 

 

如果想又开启某个view controller的全部方向旋屏支持:

复制代码 代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskAllButUpsideDown; 

 
-(BOOL)shouldAutorotate 

    return YES; 

 

从而实现了对每个view controller的单独控制。

顺便提一下,如果整个应用所有view controller都不支持旋屏,那么干脆:

复制代码 代码如下:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

     return UIInterfaceOrientationMaskPortrait; 


横竖屏切换,视图乱了怎么办?
首先,我们必须了解一下下列4种状态,它们被用来描述设备旋转方向:

 

iOS开发中控制屏幕旋转的编写方法小结

对于旋屏的处理,大致分为如下几种情况和思路:
也许,你不需要旋屏支持,而希望锁定屏幕

复制代码 代码如下:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return NO; 


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

复制代码 代码如下:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
       return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 

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

复制代码 代码如下:

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

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
        NSLog(@"现在是竖屏"); 
        [btn setFrame:CGRectMake(213, 442, 340, 46)]; 
    } 
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
        NSLog(@"现在是横屏"); 
        [btn setFrame:CGRectMake(280, 322, 460, 35)]; 
    } 


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

复制代码 代码如下:

- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UIDevice *device = [UIDevice currentDevice];  
    [device beginGeneratingDeviceOrientationNotifications]; 
    //利用 NSNotificationCenter 获得旋转信号 UIDeviceOrientationDidChangeNotification 
    NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];  
    [ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device]; 

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(void)rotation_btn:(float)n 

    UIButton *robtn = self.btn; 
    robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0); 

 
-(void)orientationChanged 

    UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation]; 
     
    switch (orientaiton) { 
        caseUIDeviceOrientationPortrait:              
            [self  rotation_btn:0.0]; 
            break; 
        caseUIDeviceOrientationPortraitUpsideDown:   
            [self  rotation_btn:90.0*2]; 
            break; 
        caseUIDeviceOrientationLandscapeLeft:      
            [self  rotation_btn:90.0*3]; 
            break; 
        caseUIDeviceOrientationLandscapeRight:   
            [self  rotation_btn:90.0]; 
            break; 
        default: 
            break; 
    } 


也许,你需要autoresizesSubviews = YES
也许,你希望横竖屏有不同的布局效果,需要准备2份Subview,在不同状态去替换
当然不要忘记,需要调节设定图示中的1、2处,

 

iOS开发中控制屏幕旋转的编写方法小结

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

延伸 · 阅读

精彩推荐
  • IOSiOS实现控制屏幕常亮不变暗的方法示例

    iOS实现控制屏幕常亮不变暗的方法示例

    最近在工作中遇到了要将iOS屏幕保持常亮的需求,所以下面这篇文章主要给大家介绍了关于利用iOS如何实现控制屏幕常亮不变暗的方法,文中给出了详细的...

    随风13332021-04-02
  • IOS详解iOS中多个网络请求的同步问题总结

    详解iOS中多个网络请求的同步问题总结

    这篇文章主要介绍了详解iOS中多个网络请求的同步问题总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    liang199111302021-03-15
  • IOSiOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)

    iOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和

    这篇文章主要介绍了iOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)的相关资料,需要的朋友可以参考下...

    CodingFire13652021-02-26
  • IOSiOS开发技巧之状态栏字体颜色的设置方法

    iOS开发技巧之状态栏字体颜色的设置方法

    有时候我们需要根据不同的背景修改状态栏字体的颜色,下面这篇文章主要给大家介绍了关于iOS开发技巧之状态栏字体颜色的设置方法,文中通过示例代码...

    梦想家-mxj8922021-05-10
  • IOSiOS自定义UICollectionViewFlowLayout实现图片浏览效果

    iOS自定义UICollectionViewFlowLayout实现图片浏览效果

    这篇文章主要介绍了iOS自定义UICollectionViewFlowLayout实现图片浏览效果的相关资料,需要的朋友可以参考下...

    jiangamh8882021-01-11
  • IOSiOS中MD5加密算法的介绍和使用

    iOS中MD5加密算法的介绍和使用

    MD5加密是最常用的加密方法之一,是从一段字符串中通过相应特征生成一段32位的数字字母混合码。对输入信息生成唯一的128位散列值(32个字符)。这篇文...

    LYSNote5432021-02-04
  • IOSiOS开发之视图切换

    iOS开发之视图切换

    在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单。在iOS开发中常用的视图切换有三种,今天我们将...

    执着丶执念5272021-01-16
  • IOSiOS中UILabel实现长按复制功能实例代码

    iOS中UILabel实现长按复制功能实例代码

    在iOS开发过程中,有时候会用到UILabel展示的内容,那么就设计到点击UILabel复制它上面展示的内容的功能,也就是Label长按复制功能,下面这篇文章主要给大...

    devilx12792021-04-02