Iphone画面回転

4124 ワード

この例は、携帯電話の画面方向が変更されたときにビューを再配置したい(ここではbutton)
1.View-based Applicationプロジェクトを作成し、ViewウィンドウにRound Rect Buttonビューを追加し、サイズチェッカで位置を設定し、Viewウィンドウの右上隅の矢印アイコンをクリックしてウィンドウ方向を回転させ、buttonを再配置します.この2つの位置は任意に定義され、異なる位置に区別できれば、コードに使用されるため、この2つの位置のデータを覚えてください.
2.はい.hヘッダファイルにUIButtonを1つ指定し、2つのメソッドを追加します.この2つのメソッドは後で説明します.
[cpp] view plain copy print ?
#import    
  •   

  •   
  • @interface ChangeOrientation : UIViewController {  

  •     IBOutlet UIButton *mybutton;  
  •       

  • }  
  • @property(nonatomic,retain)UIButton *mybutton;  

  •   
  • -(void)positionViews;  

  •   
  • -(IBAction)makeChange;  

  • @end  
    #import <UIKit/UIKit.h>
    
    
    @interface ChangeOrientation : UIViewController {
        IBOutlet UIButton *mybutton;
        
    }
    @property(nonatomic,retain)UIButton *mybutton;
    
    -(void)positionViews;
    
    -(IBAction)makeChange;
    @end

    3.携帯電話にすべての回転方向をサポートさせるには、自動生成方法を修正し、return YES:
    [cpp] view plain copy print ?
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  • {  

  •     return YES;  
  • }  
  • - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }

    4.現在の画面の方向に応じてbuttonの位置を変更する方法を追加する.hヘッダファイル定義済み:
    [cpp] view plain copy print ?
    //現在の画面方向に応じて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);  
  •   

  •     }  
  •           

  • }  
    // 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の位置を変更できます.
    (補足:w i l l n i m a t e F i r s t HalfOfRotationToInterfaceOrientation:イベントはViewウィンドウが回転する前に促されます)
    [cpp] view plain copy print ?
    //画面が半分に回転したときに促す方法
  • -(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{  

  •     [self positionViews];  
  •      

  • }  
    // 
    -(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
        [self positionViews];
       
    }
    
    6.ウィンドウのロードが完了したらpositionViewメソッドを呼び出して、現在の画面方向のbuttonの位置を特定します.
    [cpp] view plain copy print ?
    - (void)viewDidLoad  
  • {  

  •     [self positionViews];  
  •     [super viewDidLoad];  

  • }  
    - (void)viewDidLoad
    {
        [self positionViews];
        [super viewDidLoad];
    }

    7.buttonのクリック方法(この方法は.hヘッダファイルで定義されている)を追加し、このbuttonをクリックすると画面の方向が動的に変更され、コードは以下の通りである.
    [cpp] view plain copy print ?
    //buttonをクリックして画面方向を動的に変更する
  • -(IBAction)makeChange{  

  •     [[UIDevice currentDevice]setOrientation:UIInterfaceOrientationLandscapeLeft];  
  •   

  • }  
    // button 
    -(IBAction)makeChange{
        [[UIDevice currentDevice]setOrientation:UIInterfaceOrientationLandscapeLeft];
    
    }