IPhone Dev Notes - 2 - 屏幕旋转

参照IPhone4 基础开发教程第五章:自动旋转和自动调整大小


旋转屏幕将改变界面的长宽和显示方向,这种改变甚至可以使得程序看上去不可用。

这使得程序需要对屏幕的旋转进行响应,来更好的适应当前的屏幕状态。

一般有3中方法来处理屏幕旋转:

1. 自动调整

2. 手动调整

3. 切换视图


屏幕分辨率

iPhone: 320 px * 480 px

iPad:768 px * 1024 px

状态栏存在的情况下,将减少:20 px


自动调整


通过在Interface Builder中为每个控件设置大小和位置是否固定或由系统计算来完成。

该调整通过大小检查器完成。


手动调整


在收到屏幕旋转的消息后,通过代码调整屏幕中每个控件的大小和位置


//

//  test06ViewController.h

//  test06

//

//  Created by lily bear on 12-1-30.

//  Copyright 2012 lily. All rights reserved.

//


#import


@interface test06ViewController :UIViewController {

    //屏幕中的六个按钮

    UIButton *button1;

    UIButton *button2;

    UIButton *button3;

    UIButton *button4;

    UIButton *button5;

    UIButton *button6;

}


@property (nonatomic,retain)IBOutletUIButton *button1;

@property (nonatomic,retain)IBOutletUIButton *button2;

@property (nonatomic,retain)IBOutletUIButton *button3;

@property (nonatomic,retain)IBOutletUIButton *button4;

@property (nonatomic,retain)IBOutletUIButton *button5;

@property (nonatomic,retain)IBOutletUIButton *button6;



@end


//

//  test06ViewController.m

//  test06

//

//  Created by lily bear on 12-1-30.

//  Copyright 2012 lily. All rights reserved.

//


#import"test06ViewController.h"


@implementation test06ViewController


@synthesize button1;

@synthesize button2;

@synthesize button3;

@synthesize button4;

@synthesize button5;

@synthesize button6;



- (void)dealloc

{

    [button1 release];

    [button2 release];

    [button3 release];

    [button4 release];

    [button5 release];

    [button6 release];

    [super dealloc];

}


- (void)didReceiveMemoryWarning

{

   // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

   // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


/*

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad

{

    [super viewDidLoad];

}

*/


- (void)viewDidUnload

{

    self.button1 = nil;

    self.button2 = nil;

    self.button3 = nil;

    self.button4 = nil;

    self.button5 = nil;

    self.button6 = nil;

    [super view DidUnload];

   // Release any retained subviews of the main view.

   // e.g. self.myOutlet = nil;

}


//检验屏幕是否可以旋转

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

   // Return YES for supported orientations

   /*return (interfaceOrientation == UIInterfaceOrientationPortrait);*/

   //返回值表示应用允许的屏幕旋转的状态

   return YES;

}


//修改屏幕方向转换后的界面布局

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

    //屏幕竖直或反向竖直时

    if(toInterfaceOrientation == UIInterfaceOrientationPortrait

       || toInterfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown){

       //为每个控件重新设置位置

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

    }

}


@end


切换视图


拥有更复杂界面的应用程序,适合使用视图切换来应对屏幕旋转
在给视图变换带来方便的时候,当然也会有工作量的增加,需要准备多个视图。

#import


//宏定义值到角度值得变换

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


@interface test07ViewController :UIViewController {

    //水平屏幕状态下的视图

    UIView *landscape;

    //竖直屏幕状态下的视图

    UIView *portrait;

    

    //每个视图上的按钮

    UIButton *landscapeFooButton;

    UIButton *portraitFooButton;

    

    UIButton *landscapeBarButton;

    UIButton *portraitBarButton;

}


@property (nonatomic,retain)IBOutletUIView *landscape;

@property (nonatomic,retain)IBOutletUIView *portrait;

@property (nonatomic,retain)IBOutletUIButton *landscapeFooButton;

@property (nonatomic,retain)IBOutletUIButton *portraitFooButton;

@property (nonatomic,retain)IBOutletUIButton *landscapeBarButton;

@property (nonatomic,retain)IBOutletUIButton *portraitBarButton;


- (IBAction)buttonPressed:(id)sender;


@end


//

//  test07ViewController.m

//  test07

//

//  Created by lily bear on 12-1-30.

//  Copyright 2012 lily. All rights reserved.

//


#import"test07ViewController.h"


@implementation test07ViewController


@synthesize landscape;

@synthesize portrait;

@synthesize landscapeFooButton;

@synthesize portraitFooButton;

@synthesize landscapeBarButton;

@synthesize portraitBarButton;


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

   

   if (toInterfaceOrientation ==UIInterfaceOrientationPortrait){

       //将view设置为竖直的屏幕 

        self.view = self.portrait;

       self.view.transform =CGAffineTransformIdentity;

       //旋转屏幕角度,以适应当前视图

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

       //设置视图大小

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

    }

   elseif(toInterfaceOrientation ==UIInterfaceOrientationLandscapeLeft){

        self.view = self.landscape;

       self.view.transform =CGAffineTransformIdentity;

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

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

    }

   elseif(toInterfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown){

        self.view = self.portrait;

       self.view.transform =CGAffineTransformIdentity;

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

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

    }

   elseif(toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight){

        self.view = self.landscape;

       self.view.transform =CGAffineTransformIdentity;

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

        self.view.bounds =CGRectMake(0.0,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;

    }

}


- (void)dealloc

{

    [landscape release];

    [portrait release];

    [landscapeFooButton release];

    [portraitFooButton release];

    [landscapeBarButton release];

    [portraitBarButton release];

    [superdealloc];

}


- (void)didReceiveMemoryWarning

{

   // Releases the view if it doesn't have a superview.

    [superdidReceiveMemoryWarning];

    

   // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


/*

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad

{

    [super viewDidLoad];

}

*/


- (void)viewDidUnload

{

    self.landscape = nil;

    self.portrait = nil;

   self.landscapeFooButton =nil;

   self.portraitFooButton =nil;

   self.landscapeBarButton =nil;

   self.portraitBarButton =nil;

    [superviewDidUnload];

   // Release any retained subviews of the main view.

   // e.g. self.myOutlet = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

   // Return YES for supported orientations

   //return (interfaceOrientation == UIInterfaceOrientationPortrait);

   return YES;

}


@end

你可能感兴趣的:(iOs,iphone,button,uiview,interface,工作)