关于 iOS 转屏的问题
之前做过 iPad 项目的开发,在项目使用到转屏的情况很多,使用的主要方法如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.isPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
self.isPortrait = UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
[self relayoutWhenRotare];
}
当然, 你也可以使用通知
//旋转屏幕通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onDeviceOrientationChange)
name:UIDeviceOrientationDidChangeNotification object:nil];
/**
* 旋转屏幕通知
*/
- (void)onDeviceOrientationChange{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;
switch (interfaceOrientation) {
case UIInterfaceOrientationPortraitUpsideDown:{
NSLog(@"第3个旋转方向---电池栏在下");
}
break;
case UIInterfaceOrientationPortrait:{
NSLog(@"第0个旋转方向---电池栏在上");
}
break;
case UIInterfaceOrientationLandscapeLeft:{
NSLog(@"第2个旋转方向---电池栏在左");
}
break;
case UIInterfaceOrientationLandscapeRight:{
NSLog(@"第1个旋转方向---电池栏在右");
}
break;
default:
break;
}
}
不要忘记在项目中做配置如下
本文介绍了一个iOS开发者在处理iPad项目转屏操作时采用的方法。通过重写viewDidLoad和willRotateToInterfaceOrientation方法,确保了不同方向下的正确布局。此外,还介绍了如何通过监听设备方向改变的通知来实现屏幕旋转。
4051

被折叠的 条评论
为什么被折叠?



