IPhone学习笔记五-----自动旋转和自动调整大小

公司最近忙这写一个连续打印的,被折磨了几天,已经大概出现雏形了,今天开始继续学习,不过好多都忘记了,上次遇到的一个问题是怎么去自动旋转,就一直停在这里。对自己的意志力感觉还是不行。不过经过询问终于知道问题所在了。

好就没写,所以还是一步一步的来。

前面一章关于xcode4.2已经有图描述,这里就不用图。

添加新项目,注意以前书上写的基于视图的应用程序模板,也就是现在的single view application项目,添加AutosizeNew项目名。

注意在创建的时候不要去选择use storyboard,否则就找不到xib文件。对于我这个初学者来说,还是挺讨厌的,因为要对这书来偷笑

首先指定旋转支持,找到ViewController.m文件

 // Return YES for supported orientations

    //支持两个方向中的纵向模式和横向模式,但是不支持旋转到倒置的纵向模式

   return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);

   //对于IPhone模板默认仅支持一种自动旋转方向 UIInterfaceOrientationPortrait

    //而对于IPad默认将支持将有所不同


然后创建6个按钮,运行,在模拟器中就可以选择向左旋转或向右旋转等,我上次就是找不到那里旋转,实际上就是在标题上里面选择,

在硬件里面,这个我可是找了很久哭,不要笑我,我很弱智吧。


大小调整器用于设置对象的自动调整属性。内部正方形的红色箭头表示选定对象内部的水平和垂直空间。

如果水平箭头是实线则可在调整窗口大小时自由更改对象的宽度,如果水平箭头是虚线,则IPhone会尽可能将对象的宽度保持原始值。

设置自动调整属性后,就6个按钮就可全部显示在页面上了,



当我们把按钮的尺寸更改为125*125后就会感觉按钮重叠在一起了。

声明输出口;

@interface ViewController :UIViewController{

    UIButton *button1;

    UIButton *button2;

    UIButton *button3;

    UIButton *button4;

    UIButton *button5;

    UIButton *button6;

}

@property (nonatomic,retain)IBOutlet UIButton *button1;

@property (nonatomic,retain)IBOutlet UIButton *button2;

@property (nonatomic,retain)IBOutlet UIButton *button3;

@property (nonatomic,retain)IBOutlet UIButton *button4;

@property (nonatomic,retain)IBOutlet UIButton *button5;

@property (nonatomic,retain)IBOutlet UIButton *button6;


然后按住control键并将File's Owner图标拖到相应的按钮上,注意,应该在添加输出口之后保存。

//要移动按钮一遍充分利用空间,需要覆盖willAnimateRotationToInterfaceOrientation:duration

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration

{

   if(interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)

    {

        //所有视图的大小和位置都在frame属性中指定,该属性是一个类型为CGRect的结构,CGRectMake是苹果公司提供的一个函数,支持通过指定xy位置以及widthheight来轻松创建CGRect

        //CG开头包含与图形和绘制相关的代码。

       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);

    }

}

对于简单的页面,没有必要将控件来回调整,因为调整的时间我们可以创建出新的视图。所以,我们有必要去切换视图,我们只要调用不同的视图就可以解决上面的问题。

创建新项目Swap ,声明操作和输出口

//用于在度数和弧度之间的转换

#define degreeToRadians(x) (M_PI *(x)/180.0)


@interface ViewController :UIViewController

{

    UIView *landscape;

    UIView *portrait;

    

   //Foo

    UIButton *landscapeFooButton;

    UIButton *portraitFooButton;

    

   //Bar

    UIButton *landscapeBarButton;

    UIButton *portraitBarButton;

}


@property (nonatomic,retain)IBOutlet UIView *landscape;

@property (nonatomic,retain)IBOutlet UIView *protrait;

@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;

设计两个视图,删除默认视图,拖入两个新视图并重命名。(如何重命名在右侧找到 show the identity inspector 第三个按钮,在下面的identity下面的label中输入你想要的名字就可以了)

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

{

   if(interfaceOrientation==UIInterfaceOrientationPortrait)

    {

        self.view=self.protrait;

       self.view.transform=CGAffineTransformIdentity;

       self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(0));

        self.view.bounds=CGRectMake(0,0, 320.0, 460.0);

    }

   else if(interfaceOrientation==UIInterfaceOrientationLandscapeLeft)

    {

       self.view=self.landscape;

       self.view.transform=CGAffineTransformIdentity;

       self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(-90));

        self.view.bounds=CGRectMake(0,0, 480.0, 300.0);

    }

   else if(interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)

    {

        self.view=self.protrait;

       self.view.transform=CGAffineTransformIdentity;

       self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(180));

        self.view.bounds=CGRectMake(0,0, 320.0, 460.0);

        

    }

   else if(interfaceOrientation==UIInterfaceOrientationLandscapeRight)

    {

       self.view=self.landscape;

       self.view.transform=CGAffineTransformIdentity;

       self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(90));

        self.view.bounds=CGRectMake(0,0, 480.0, 300.0);

   }

根据旋转的位置调用不同的视图,并且设置视图的尺寸。和旋转角度

-(IBAction)buttonPressed:(id)sender

{

   if(sender==portraitFooButton||sender==landscapeFooButton)

    {

       portraitFooButton.hidden=YES;

       landscapeFooButton.hidden=YES;

    }

    else

    {

       portraitBarButton.hidden=YES;

       landscapeBarButton.hidden=YES;

    }

    

}

触发事件

dealloc方法主要是清除内存。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值