iPhone开发之屏幕旋转

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来创建一个旋转变换。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值