您的位置:首页 > 移动开发 > IOS开发

需求:IOS横竖屏幕切换

2015-03-25 15:20 344 查看
需求:IOS监听横竖屏幕切换,并做相应布局调整。

实现:

1、设置监听横竖屏切换动作

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
orientation = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation; //This is more reliable than (self.interfaceOrientation) and [[UIDevice currentDevice] orientation] (which may give a faceup type value)
if (orientation == UIDeviceOrientationUnknown || orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown)
{
orientation = UIDeviceOrientationPortrait;
}

2、UI布局处理

-(void)didRotate:(NSNotification *)notification{

UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
if (newOrientation != UIDeviceOrientationPortraitUpsideDown && newOrientation != UIDeviceOrientationUnknown && newOrientation != UIDeviceOrientationFaceUp && newOrientation != UIDeviceOrientationFaceDown && newOrientation != orientation){
orientation = newOrientation;
[self initWindowViews];
}
}
3、兼容性问题(网上例子,但没有测试),不加暂没发现任何问题。

// iOS5.0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return UIInterfaceOrientationMaskAllButUpsideDown; // 可以修改为任何方向
}

-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown; // 可以修改为任何方向
}

-(BOOL)shouldAutorotate{
return YES;
}

参考网址:

http://blog.csdn.net/openglnewbee/article/details/40404495

例子

https://github.com/Thinkerfans/ios-screenrotate-demo.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 横竖屏切换