当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS开发中控制屏幕旋转的编写方法小结

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

足球比分182323点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; 

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

2015102495412717.png (408×178)

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

复制代码 代码如下:

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

2015102495438507.png (258×296)

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网