iPhone中有重力感应我们可以通过旋转手机使屏幕旋转。但是屏幕旋转后页面的布局需要注意。
UIInterfaceOrientationPortrait,垂直向上
UIInterfaceOrientationPortraitUpsideDown,垂直倒放。
UIInterfaceOrientationLandscapeLeft,水平向左。
UIInterfaceOrientationLandscapeRight,水平向右。
注意:以手机屏幕为参照物的向左、向右。
开启旋转
可以通过shouldAutorotateToInterfaceOrientation:方法开启或禁止旋转。
允许任何方向的旋转:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
垂直向上和水平向右
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
触发旋转事件
我们可以在屏幕旋转的时候触发很多事件,其中willAnimateRotationToInterfaceOrientation是我们常用的事件,这个事件是在即将开始屏幕旋转动画的时候触发。
- (void)willAnimateRotationToInterfaceOrientation:
{
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
txtField.text = @"垂直显示。";
} else {
txtField.text = @"水平显示。";
}
}
由于屏幕旋转后,控件的位置会发生变化,我们要让这些控件能够摆放相对比较合理。我们可以通过“Control Size”调整。其中红色实线代表绝对位置,红色虚线代表相对位置。
横屏竖屏切换不同视图
.h文件
#import <UIKit/UIKit.h>
#define degreesToRadians(X) (M_PI * (x) / 180.0)
@interface SwapViewController : UIViewController {
UIView *landscape;
UIView *portrait;
UIButton *landscapeFooButton;//Foo
UIButton *portraitFooButton;
UIButton *landscapeBarButton;//Bar
UIButton *portraitBarButton;
}
@property (nonatomic retain) IBOutlet UIView *landscape;
@property (nonatomic retain) IBOutlet UIView *portrait;
@property (nonatomic retain) IBOutlet UIButton *landscapeFooButton;
@property (nonatomic retain) IBOutlet UIButton *portraitFooButton;
@property (nonatomic retain) IBOutlet UIButton *landscapeBarButton;
@property (nonatomic retain) IBOutlet UIButton *portraitBarButton;
-(IBAction) buttonPressed:(id)sender;
@end
#define degreesToRadians(X) (M_PI * (x) /180.0)这是一个宏,用于在度数和弧度之间的转换。
landscape水平视图,portrait垂直视图;
landscapeFooButton水平视图中Foo按钮;
portraitFooButton垂直视图中Foo按钮;
landscapeBarButton水平视图中Bar按钮;
portraitBarButton垂直视图中Bar按钮。
.m文件
#import "SwapViewController.h”
@implementation SwapViewController
@synthesize landscape;
@synthesize portrait;
@synthesize landscapeFooButton;
@synthesize portraitFooButton;
@synthesize landscapeBarButton;
@synthesize portraitBarButton;
!
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration{
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(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);
}
}
willAnimateRotationToInterfaceOrientationduration:这个方法来自我们重写的一个父类,这个方法在旋转开始之后与旋转实际发生之前被调用。 CGAffineTransformIdentity,置变换属性。CGAffineTransformMakeRotation来创建一个旋转变换。