본문 바로가기

DEV life/DEV.mobile

날로 먹는 iPhone dev 일기 #2

Touch

어떤 형태로건 사용자의 입력이 없다면 그것을 '게임'이라고 부를 수 있을 까요?

사용자의 입력이라는 것이, 일반적인 게임의 조작성에서 조금 멀리 떨어져 있을 수는 있어도-비쥬얼 노블 류 처럼 OK 버튼이나 푹푹 눌러대거나, NDS의 닌텐독스나 대합주에서 처럼 마이크에 소리를 지른다거나 등등등-어찌 되었건 '게임'이라는 어플리케이션을 구성하는데 외부의 조작을 처리하는 것은 반드시 필요한 것이 되겠습니다. 

 iPhone은, 설명할 필요도 없이, 메인 입력으로 터치 인터페이스를 사용하죠. 이것을 처리하기 위해서 필요한 것은, 프로젝트에 UIKit framework가 포함되어 있을 것입니다.


touch를 제어하는 방법을 잘 다룬 예제로는 Apple Sample Codes의 Touches가 있습니다. 코드를 얻은 후, MyView.m 파일 중반을 보시면 touch event 를 다루는 이벤트 핸들러 메소드들이 있습니다.

이를 간단히 정리해보면-

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;


이름을 보니 이놈이 뭐하는 놈인지 설명할 필요가 없어졌습니다.

그래도 간단히 짚고 넘어가죠^^;;


// Handles the start of a touch

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSUInteger numTaps = [[touches anyObject] tapCount]; // ==> 현재 터치되어있는 수를 구하는 코드

// ...중략


// Enumerate through all the touch objects.

NSUInteger touchCount = 0;

for (UITouch *touch in touches) {

    // Send to the dispatch method, which will make sure the appropriate subview is acted upon

[self dispatchFirstTouchAtPoint:[touch locationInView:self] forEvent:nil];

// ==> 모든 터치에 대해, 터치 위치와 이벤트 등에 따라 처리해줍니다.

touchCount++;  

}

}


-(void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event

{

// ...생략-_-

}


이놈 까지 봤으니 이후는 더더욱 설명할 것이 없을 것입니다.


// Handles the continuation of a touch.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{  

// ...생략

// Enumerates through all touch objects

   for (UITouch *touch in touches) {

// Send to the dispatch method, which will make sure the appropriate subview is acted upon

  [self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self]];

}

// ...생략

}


-(void) dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position

{

// ...생략

}


// Handles the end of a touch event.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    // Enumerates through all touch object

    for (UITouch *touch in touches){

// Sends to the dispatch method, which will make sure the appropriate subview is acted upon

[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:self]];

}

}


-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position

{   

// ...생략

}


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    // Enumerates through all touch object

    for (UITouch *touch in touches){

// Sends to the dispatch method, which will make sure the appropriate subview is acted upon

[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:self]];

}

}


참 쉽죠잉?-_-  터치의 처리 자체는 간단한 문제지만, 이후 구현하고자 하는 게임의 인터페이스 디자인에 따라 이벤트의 처리를 보다 효율적으로, 그리고 직관적으로 설계하기 위한 연구가 필요할 것입니다. 다음은 사운드 출력 고고싱~

* iPhone Simulator에서의 멀티터치 입력은, option키를 누르면 가능합니다.


'DEV life > DEV.mobile' 카테고리의 다른 글

SKT의 앱스토어, 성공할까?  (26) 2009.04.14
2D 프레임버퍼의 제어  (2) 2009.04.07
날로 먹는 iPhone dev 일기 #1  (2) 2009.04.01
날로 먹는 iPhone dev 일기 #0  (2) 2009.03.30
게임 플랫폼으로서의 iPhone  (2) 2009.03.24