Ios5(三)屏幕的旋转和大小设置;
一. 设置自动转屏的一般原则:
iphone类应用,如果为了增强用户体验,可以增加自动转屏功能,ipad应用,一般都应该添加自动转屏功能;
二. 自动转屏的实现方法:
1. 自动调整;
2. 看到视图旋转提示时,手动调整视图对象位置;
3. 在InterfaceBuilder中为视图设置两个不同的版本,一种为纵向模式,一种是横向模式;
三. 自动调整属性处理:
1. 打开项目设置,看:”Supported Device Orientations”可以更改支持的面向;即更改的info.plist的设置;
2. 在viewController.m文件中,添加方法:系统通过此方法询问视图控制器是否应该旋转到指定方向;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return(toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
3. ios四个方向分别对应的常量:
UIInterfaceOrientationPortraitUpsideDown;
UIInterfaceOrientationPortrait;
UIInterfaceOrientationLandscapeLeft;
UIInterfaceOrientationLandscapeRight;
4. 在xib,加入六个button,查看视图效果
5. 2
四. 旋转时重构视图:
1. 分别将六个按钮关联六个输出口,分别命名为:ul,ur,l,r,ll,lr;
2. 在旋转时重新构图:.m文件中重写方法:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration{
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
ul.frame = CGRectMake(20, 20, 125, 125);
ur.frame = CGRectMake(175, 20, 125, 125);
l.frame = CGRectMake(20, 168, 125, 125);
r.frame = CGRectMake(175, 168, 125, 125);
ll.frame = CGRectMake(20, 315, 125, 125);
lr.frame = CGRectMake(175, 315, 125, 125);
}else{
ul.frame = CGRectMake(20, 20, 125, 125);
ur.frame = CGRectMake(20, 155, 125, 125);
l.frame = CGRectMake(177, 20, 125, 125);
r.frame = CGRectMake(177, 155, 125, 125);
ll.frame = CGRectMake(328, 20, 125, 125);
lr.frame = CGRectMake(328, 155, 125, 125);
}
}
3. 所有的视图的大小和位置都在frame属性中指定,该属性是一个类型为CGRect的结构;
4. 注意设备朝向的判定,可以用方法,也可以用字段;
五. 旋转时切换视图:
1. 重新建一个单视图项目,并复制视图,分别在两个视图中添加两个那妞,如图:
2. 定义一个角度弧度转换宏:#definedegreesToRadians(x)(M_PI*(x)/180.0)
3. 重现旋转的响应函数:
2- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration{
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
self.view = portraint;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}else if(UIInterfaceOrientationLandscapeLeft ==toInterfaceOrientation){
self.view = landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}else{
self.view = landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
}
4. 添加属性和操作:将两个视图的foo按钮同时关联outlet connection属性foos;Bar关联到bars;
为两个视图的所有按钮关联动作:buttonPressed:
- (IBAction)buttonTapped:(id)sender{
NSString *mes = nil;
if([self.foos containsObject:sender]){
mes= @"Foo Button Pressed";
}
else{
mes= @"Bar Button Pressed";
}
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:mesmessage:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
IOS有输出口集合功能,可以同时指向多个元素,而输出口只能指向一个元素;
判断:
[self.foos containsObject:sender]返回BOOL值类型;
5. 如果希望点击按钮使两个按钮都消失可以使用数组的快速遍历:
- (IBAction)buttonTapped:(id)sender{
if([foos containsObject:sender]){
for(UIButton*onbutton in foos){
onbutton.hidden = YES;
}
}else{
for(UIButton*onbutton in bars){
onbutton.hidden = YES;
}
}
}