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种状态,它们被用来描述设备旋转方向:
对于旋屏的处理,大致分为如下几种情况和思路:
也许,你不需要旋屏支持,而希望锁定屏幕
-(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处,
来帮助我们完成自己想要的适应效果。example 动画呈现的很清晰,^_^ 我就不再啰嗦了。