본문 바로가기
iPhone 개발

iPhone 개발 학습 2회...View 전환

by GoodDev 2011. 8. 30.
기본적인 작업 방식은 Nib File 을 사용하지 않고 작업을 진행 하였습니다.

개발 기본 개념은 아래의 사이트를 참조 하였습니다.
참조 사이트 :  http://onstory.egloos.com/367869
자세한 구조 설명 및 설계 부분은 참조 사이트를 방문하시기 바랍니다.


- UIViewController subclass 3 개 추가
- Objective-C Class UIView 3개 추가

RootViewController - root_view
ViewController - view1_1
ViewController2 - view1_2

관계를 형성하고 있다.
















작업 순서는 View -> Controller 순서로 작업하자.

가. View 작업

view1_1, view1_2 는 필요한 작업이 없다. 간단히 배경 색만 바꿔 주자.

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code.

       self.backgroundColor = [ UIColor grayColor];  // view1_1

       self
.backgroundColor = [ UIColor yellowColor];  // view1_2

    }

    return self;



root_view 는 버튼을 생성하자.

-- root_view.h

#import <UIKit/UIKit.h>

@interface root_view : UIView {

UIButton *changeBtn;

}

@property (nonatomic,retain) UIButton *changeBtn;

@end


-- root_view.m

#import "root_view.h"

@implementation root_view

@synthesize changeBtn;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code.

self.backgroundColor = [ UIColor whiteColor];

// Button생성

changeBtn = [ self makeButton:CGRectMake(50,50,200,40) text:@"change" tag:10 ];

[ self addSubview:changeBtn ];

 }

    return self;

}


-(UIButton*) makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag

{

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setTitle:text forState:UIControlStateNormal];

[btn setFrame:rect];

[btn setTag:tag];

return btn;

}


// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code.

}


- (void)dealloc {

[changeBtn release];

    [super dealloc];

}

@end 

 나. Controller 작업

ViewController 1 과 ViewController 2는   view 객체를 생성하고 컨트롤러에 등록한다.

ViewController.h

#import
<UIKit/UIKit.h>

@interface ViewController1 : UIViewController {

UIView* vw;

}

@property (nonatomic, retain) UIView* vw;

@end

ViewController.m

- (void)loadView {

vw = [ [view1_1 alloc] initWithFrame:CGRectMake(0,0,320,460)]; // 화면크기 설정 필수

self.view = vw;

}

 
RootViewController 에서는 root_view를 생성하고, root_view 의 버튼의  이벤트 함수를 등록하며 이벤트시에 뷰가 전환되

는 코드를 생성한다.

RootViewController.h

// 코드의 원천 소스는 http://onstory.egloos.com/367869 를 참조하였습니다.  

#import <UIKit/UIKit.h>

#import "root_view.h"

#import "ViewController1.h"

#import "ViewController2.h"


@interface RootViewController : UIViewController {

ViewController1* viewctl1;

ViewController2* viewctl2;

UIViewController* to;

UIViewController* from;

root_view *root;

UIButton* _transitionButton;

}


@property (nonatomic,retain) ViewController1 *viewctl1;

@property (nonatomic,retain) ViewController2 *viewctl2;


@property (nonatomic,retain) UIViewController *to;

@property (nonatomic,retain) UIViewController *from;


@property (nonatomic,retain) root_view *root;

@property(nonatomic,retain) UIButton *_transitionButton;


-(IBAction)clickButton:(UIButton *)sender;

@end



RootViewController.m

// 코드의 원천 소스는 http://onstory.egloos.com/367869 를 참조하였습니다. 

#import "RootViewController.h"

#import "view1_1.h"

@implementation

RootViewController

@synthesize viewctl1;

@synthesize viewctl2;

@synthesize root;

@synthesize to;

@synthesize from;

@synthesize _transitionButton;


// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView {

self.viewctl1 = [ [ ViewController1 alloc ] init ];

self.viewctl2 = [ [ ViewController2 alloc ] init ];

// 루트 설정

root = [[ root_view alloc ] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

_transitionButton = root.changeBtn;

self.view = root;

// register Button Callback 

[ _transitionButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside ];


[self.view insertSubview:viewctl1.view belowSubview:_transitionButton];

from = viewctl1;

to = viewctl2;

}


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

- (void)viewDidLoad {

    [super viewDidLoad];

}



-(IBAction)clickButton:(UIButton *)sender

{

[UIView beginAnimations:nil context:NULL]; //창전환 애니메이션 시작 선언

[UIView setAnimationDuration:1]; //애니메이션 동작 시간 설정 초단위 이다.

[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

//어떤 효과로 전환을 할지 설정 한다. 밑줄파란색부분을 변경하면 전환 효과를 다르게 줄수 있다.

[to viewWillAppear:YES]; //보여질

[from viewWillDisappear:YES];//사라질

[from.view removeFromSuperview]; //현재 제거

[self.view insertSubview:to.view belowSubview:_transitionButton]; //보여질

[from viewDidDisappear:YES];

[to viewDidAppear:YES];

//여기 까지가 실제 메모리 적으로는 작업이 이미 완료 상태이다.

[UIView commitAnimations]; //해당 명령어가 사용자에게 시각적으로 보여 지게 하는 부분이다.

//창전환 버튼을 지속적으로 눌러도 계속 작동 하기 위해 임시 변수를 두고 to, from 메모리에 있는 뷰를 서로 교환해 준다.

UIViewController* tmpView;

tmpView = from;

from = to;

to= tmpView;

}


/*

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations.

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

*/


- (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.

}


- (void)viewDidUnload {

    [super viewDidUnload];

    // Release any retained subviews of the main view.

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

}


- (void)dealloc {

[ viewctl1 release ];

[ viewctl2 release ];

[ root release ];

    [super dealloc];

}

@end 

댓글