2011-01-05 63 views
2

如何在使用cocos2d的iPhone編程中做簡單的手勢識別?當你滑動屏幕時,手勢僅僅意味着手指的路徑。iPhone編程中的手勢識別

我想在用戶觸摸屏幕時使用一些簡單的手勢,如線條(八個方向),圓形,正方形和其他手勢,如'V','^','<','> 」。有沒有簡單的方法可以快速識別用戶輸入?

回答

3

iOS SDK 3.2引入了UIGestureRecognizer類來簡單識別簡單的手勢。

UIGestureRecognizer的具體子類如下:

UITapGestureRecognizer 
UIPinchGestureRecognizer 
UIRotationGestureRecognizer 
UISwipeGestureRecognizer 
UIPanGestureRecognizer 
UILongPressGestureRecognizer 

如果你需要認識更多的手勢,你可以嘗試sublassing UIGestureRecognizer,例如「對勾」的手勢。請參閱Apple的參考文檔中的子類註釋。

+0

感謝您的回覆,我將在後面有一個嘗試。 – icethawless 2011-01-11 03:04:48

1

請嘗試以下鏈接http://www.techotopia.com/index.php/An_iPhone_iOS_4_Gesture_Recognition_Tutorial_(Xcode_4) 描述手勢識別。

,並嘗試爲它:

UISwipeGestureRecognizer *recognizer; 

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; 
[[self view] addGestureRecognizer:recognizer]; 
[recognizer release]; 

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; 
[[self view] addGestureRecognizer:recognizer]; 
[recognizer release]; 

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; 
[[self view] addGestureRecognizer:recognizer]; 
[recognizer release]; 

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 
[[self view] addGestureRecognizer:recognizer]; 
[recognizer release];