Iphone屏幕旋转

该示例是想在手机屏幕方向发生改变时重新定位视图(这里是一个button)

1.创建一个View—based Application项目,并在View窗口中添加一个Round Rect Button视图,通过尺寸检查器设置其位置,然后单击View窗口右上角的箭头图标来旋转窗口方向,重新定位button,这两个位置随便定义,只要能区分在不同位置即可,记住这两个位置的数据,因为在代码里面会用到。

2.在.h头文件里面定一个UIButton,并添加两个方法,后面会解释这两个方法:

  1. #import <UIKit/UIKit.h>   
  2.   
  3.   
  4. @interface ChangeOrientation : UIViewController {  
  5.     IBOutlet UIButton *mybutton;  
  6.       
  7. }  
  8. @property(nonatomic,retain)UIButton *mybutton;  
  9.   
  10. -(void)positionViews;  
  11.   
  12. -(IBAction)makeChange;  
  13. @end  
#import <UIKit/UIKit.h>


@interface ChangeOrientation : UIViewController {
    IBOutlet UIButton *mybutton;
    
}
@property(nonatomic,retain)UIButton *mybutton;

-(void)positionViews;

-(IBAction)makeChange;
@end

3.要向让手机支持所有旋转方向,必须修改自动生成的方法,让其return YES:

  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.     return YES;  
  4. }  
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

4.添加一个根据当前屏幕的方向改变button位置的方法,该方法在.h头文件定义过:

  1. //根据当前的屏幕方向改变button的位置   
  2. -(void)positionViews{  
  3.     UIInterfaceOrientation destorientation = self.interfaceOrientation;  
  4.     if (destorientation == UIInterfaceOrientationPortrait ||   
  5.         destorientation == UIInterfaceOrientationPortraitUpsideDown) {  
  6.         mybutton.frame = CGRectMake(20, 20, 233, 37);  
  7.   
  8.     }else{  
  9.         mybutton.frame = CGRectMake(227, 243, 233, 37);  
  10.   
  11.     }  
  12.           
  13. }  
//根据当前的屏幕方向改变button的位置
-(void)positionViews{
    UIInterfaceOrientation destorientation = self.interfaceOrientation;
    if (destorientation == UIInterfaceOrientationPortrait || 
        destorientation == UIInterfaceOrientationPortraitUpsideDown) {
        mybutton.frame = CGRectMake(20, 20, 233, 37);

    }else{
        mybutton.frame = CGRectMake(227, 243, 233, 37);

    }
    	
}

5.当屏幕正在旋转的时候需要处理如下事件,这样就可以调用前面定义的方法positionViews方法改变button的位置:

(补充:willAnimateFirstHalfOfRotationToInterfaceOrientation:事件是在View窗口开始旋转前促发)

  1. //当屏幕旋转到一半的时候促发的方法   
  2. -(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{  
  3.     [self positionViews];  
  4.      
  5. }  
//当屏幕旋转到一半的时候促发的方法
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
    [self positionViews];
   
}
6.在窗口加载完毕后调用positionViews方法来定位当前屏幕方向的button的位置:

  1. - (void)viewDidLoad  
  2. {  
  3.     [self positionViews];  
  4.     [super viewDidLoad];  
  5. }  
- (void)viewDidLoad
{
    [self positionViews];
    [super viewDidLoad];
}

7.添加一个button点击方法(该方法在.h头文件中定义过),当点击这个button的时候动态改变屏幕的方向,代码如下:

  1. //点击button动态改变屏幕方向   
  2. -(IBAction)makeChange{  
  3.     [[UIDevice currentDevice]setOrientation:UIInterfaceOrientationLandscapeLeft];  
  4.   
  5. }  
//点击button动态改变屏幕方向
-(IBAction)makeChange{
    [[UIDevice currentDevice]setOrientation:UIInterfaceOrientationLandscapeLeft];

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值