Ios5(三)屏幕的旋转和大小设置;

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;

        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值