iPhone或iPad可以支持4种朝向
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
究竟支持哪几个朝向,由view controller的shouldAutorotateToInterfaceOrientation函数来指定,每当设备的朝向发生变化时,这个方法都会被调用。
对于iPhone程序,应该防止用户在通话时拿倒电话,不支持UIInterfaceOrientationPortraitUpsideDown
因此函数实现为:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
对于iPad,应该支持所有的朝向,因此函数实现为
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
return true;
}
实现对Rotation的支持有三种方法:
1. 设置UI元素的Autosize Attribute,类似于.NET form的dock
2. 重新构造UI元素大小和位置
3. 不同的朝向使用不同的view
使用Autosize Attribute
Inner box中的红色箭头和UI元素的size有关,如果横向的红色的箭头成为实线,则窗口尺寸变化时UI元素的宽度也会变化,如果横向红色箭头为虚线,则窗口宽度变化时UI元素的宽度保持不变。垂直方向同理。
Inner box周围的短线变成实线时,表明UI元素和它所在的view的边缘的距离保持不变。
下图中的UI对象的高度将会随着view的高度而变化,同时UI元素相对于View的上下边距不变
- (void)
willAnimateRotationToInterfaceOrientation
: (UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
button1.frame = CGRectMake(20, 20, 125, 125);
button2.frame = CGRectMake(175, 20, 125, 125);
button3.frame = CGRectMake(20, 168, 125, 125);
button4.frame = CGRectMake(175, 168, 125, 125);
button5.frame = CGRectMake(20, 315, 125, 125);
button6.frame = CGRectMake(175, 315, 125, 125);
}
else
{
button1.frame = CGRectMake(20, 20, 125, 125);
button2.frame = CGRectMake(20, 155, 125, 125);
button3.frame = CGRectMake(177, 20, 125, 125);
button4.frame = CGRectMake(177, 155, 125, 125);
button5.frame = CGRectMake(328, 20, 125, 125);
button6.frame = CGRectMake(328, 155, 125, 125);
}
}
切换View
为了支持两个View,需要在View controll中生成两套outlet
@interface SwapViewController : UIViewController {
UIView *landscape;
UIView *portrait;
// Foo
UIButton *landscapeFooButton;
UIButton *portraitFooButton;
// Bar
UIButton *landscapeBarButton;
UIButton *portraitBarButton;
}
在Interface builder中删除Xcode自动生成的view,从library中拖两个view 到main window上。分别命名为Portrait和Landscape.
选中File’s Owner,Control-drag 到Portrait和Landscape,连接outlet
动态加载view
#define degreesToRadians(x) (M_PI * (x) / 180.0)
- (void)
willAnimateRotationToInterfaceOrientation
: (UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));#define degreesToRadians(x) (M_PI * (x) / 180.0)
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
}