iphone的自动旋转和自动调节大小


iphone的屏幕为320*480,状态栏高度为20像素,主要显示电量,信号强度,时间等。

应用程序一般使用三种方法来实现屏幕旋转:

一、自动调整属性

二、旋转时候重构视图

三、在多个视图间进行切换



一,自动调整属性和旋转时候重构视图可以归并为一种方法,这种方法对于较复杂的视图不太适合。用到的两个比较关键的函数是

-(BOOL)shouldAutorotateToInterafceOrientation:(UIInterfaceOrientation)interfaceOrientation{

return YES;

};

这个函数时用来确定我们的应用所支持的旋转方向。如果想要支持每个方向则直接返回YES就行,还可以单独判断某一方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {

//left

}

if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {

//right

}

if (interfaceOrientation==UIInterfaceOrientationPortrait) {

//up

}

if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {

//down

}

return YES;

}

上面函数中UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight,UIInterfaceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown是四个宏,分别代表向左,向右,倒置,正常四个状态

如果只有这个函数在对应的方向返回YES,可能会导致控件布局不佳,还需要使用Interface builder来调整自动布局属性,如下图


具体的动画设置方法省去。


当然旋转还有一些函数可触发:

//旋转方向发生改变时

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

}

//视图旋转动画前一半发生之前自动调用

-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

}

//视图旋转动画后一半发生之前自动调用

-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {

}

//视图旋转之前自动调用

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

}

//视图旋转完成之后自动调用,一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

}

//视图旋转动画前一半发生之后自动调用

-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

}

我们可以通过重写函数willAnimateRotationToInterfaceOrientation:duration:,在旋转发生时候,通过硬编码来调整布局,主要是修改控件的frame属性,该属性是一个CGRect结构,可以通过苹果提供的CGRectMake函数,指定x,y坐标以及width 和heigh属性


-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
	if(toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
		button1.frame=CGRectMake(20,30,125,70);  //button1是一个控件输出口
	}else{
		button1.frame=CGRectMake(30,50,125,70);  
	}
}


第二种方法是创建多个view,在屏幕发生旋转时候,通过在view1和view2之间切换,这种方法适用于复杂的界面,当两个视图中的控件可能触发相同的操作时候,我们必须为这两个视图分别提供两个不同的输出口集,分别对应这两个视图,这会增加代码的复杂度,分别用view1和view2来赋值给视图控制器的view属性,并且要为这两个新的视图在视图控制器中提供两个视图输出口,在IB里面把它们和对应是视图连接起来,IBOultet UIView* view1,IBOultet UIView* view2,同样在函数willAnimateRotationToInterfaceOrientation:duration:,中进行修改,同时要为self.view.transform和self.view.bounds属性设置选择值,如下

#define degreeToRadians(X)  (M_PI * X/180)  //定义一个宏,用于将度数转换为弧度

//这个函数在旋转开始之后,实际旋转发生之前被调用
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
        self.view=self.view1;
        self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(0));
        self.view.bounds=CGRectMake(0.0, 0.0, 320.0, 460.0);
    }else if(toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
        self.view=self.view2;
        self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(-90));
        self.view.bounds=CGRectMake(0.0, 0.0, 480.0, 300.0);

    }else if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){
        self.view=self.view1;
        self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(180));
        self.view.bounds=CGRectMake(0.0, 0.0, 320.0, 460.0);

    }else if(toInterfaceOrientation==UIInterfaceOrientationLandscapeRight){
        self.view=self.view2;
        self.view.transform=CGAffineTransformMakeRotation(degreeToRadians(90));
        self.view.bounds=CGRectMake(0.0, 0.0, 480.0, 300.0);
        
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值