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

2016-02-19 10:56 42 1 收藏

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐iOS开发中控制屏幕旋转的编写方法小结,无聊中的都看过来。

【 tulaoshi.com - 编程语言 】

在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; 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

如果想又开启某个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种状态,它们被用来描述设备旋转方向:

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

-(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处,

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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

来源:https://www.tulaoshi.com/n/20160219/1596150.html

延伸阅读
iphone屏幕旋转关闭方法 相信刚刚到手iphone的同学都曾遇到过这个困扰,就是躺着床上看手机的时候,手机总是只能的旋转了屏幕,横过来了但是其实我们看起来并不方便,那怎么才能让屏幕不旋转呢?方法很简单,大家来看一下:   双击HOME键,下排会出现我们后台正在运行中的程序www.tulaoshi.com这个大家都知道,如图 往...
标签: Web开发
需要注意的是,这里的代码好多是针对jquery 1.32以前的版本(以后的版本已经不支持@),所以替换为空测试下即可。 //遍历option和添加、移除option function changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] option").length if(shipping.value != "CA"){ $("select[@name=ISHIPTYPE] option").each(function(){ if($(t...
Method Swizzling 原理 在Objective-C中调用一个方法,其实是向一个对象发送消息,查找消息的唯一依据是selector的名字。利用Objective-C的动态特性,可以实现在运行时偷换selector对应的方法实现,达到给方法挂钩的目的。 每个类都有一个方法列表,存放着selector的名字和方法实现的映射关系。IMP有点类似函数指针,指向具体的Method实现。...
一、快捷键的使用 经常用到的快捷键如下: 新建 shift + cmd + n     新建项目 cmd + n             新建文件 视图 option + cmd + 回车 打开助理编辑器 cmd + 回车           显示主窗口 cmd +...
一、初始化 代码如下: UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];      [self.view addSubview:myLabel]; 二、设置文字 1、设置默认文本 代码如下: NSString *text = @"标签文本"; myLabel.text = text; 效果: 2、设置标签文本(此属性是iOS6.0之后才出现,如若...

经验教程

866

收藏

72
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部