Cocos2d中,CCLayer類被設計用來獲取觸摸信息,該類實現了兩個協議:CCStandardTouchDelegate和CCTargetedTouchDelegate,我們可以使用這兩者中的任何一個來獲取觸摸事件。
相關類
首先來了解一下相關的幾個類、處理觸屏事件時操作和執行的流程
CCTouch:它封裝了觸摸點,可以通過locationInView函數返回一個CCPoint。
CCTouchDelegate:它是觸摸事件委托,就是系統捕捉到觸摸事件后交由它或者它的子類處理,所以我們在處理觸屏事件時,必須得繼承它。它封裝了下面這些處理觸屏事件的函數:
CCTargetedTouchDelegate和CCStandardTouchDelegate是CCTouchDelegate的子類,類結構圖如下:

CCStandardTouchDelegate用于處理多點觸摸;CCTargetedTouchDelegate用于處理單點觸摸。
CCTouchDispatcher:實現觸摸事件分發,它封裝了下面這兩個函數,可以把CCStandardTouchDelegate和CCTargetedTouchDelegate添加到分發列表中:
void addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority);
void addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches);
CCTouchHandler:封裝了CCTouchDelegate和其對應的優先級,優先級越高,分發的時候越容易獲得事件處理權,CCStandardTouchHandler和CCTargetedTouchHandler是它的子類。
CCLayer默認是不捕獲觸摸事件的,要使得其能夠捕獲到相應的觸摸實踐,我們需要將 isTouchEnabled 屬性設置成 YES:
self.isTouchEnabled = YES;
當設置好屬性后,就可以使用很多方法來捕獲觸摸事件。CCLayer默認使用的CCStandardTouchDelegate,該協議的方法有:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
我們可以看到,該協議中的方法于CocoaTouch的用法類似。
方便多點觸摸的處理
觸摸信息可以直接從touches參數中獲取
也可使用event參數來獲取
除了CCStandardTouchDelegate,也可以使用CCTargetedTouchDelegate來捕獲觸摸。該協議定義如下:
@protocol CCTargetedTouchDelegate
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
@optional
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
-(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
@end
使用CCTargetedTouchDelegate有兩點優點:
你不需要處理NSSets,事件的發送者已經將NSSets分割,確保在每次調用時有且只有一個UITouch對象。單點觸摸
如果在ccTouchBegin中返回True,就可以對當前的UITouch對象具有所有權,這樣就可以在后續的move/ended/cancelled方法中確認時當前的觸摸,這樣就可以在多點觸摸中減少工作量。
響應觸摸的輔助方法
于通常直接在代碼中添加要響應的方法外,還需要多一步操作。CCLayer的定義中有一個函數:(以下是standard的方法)
-(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:0]; }
該函數的作用就是指定需要使用哪種協議來處理觸摸事件,上述的代碼就是指定使用CCStandardTouchDelegate。為了不使用默認的協議,需要在CCLayer中重寫該函數:(以下是target方法)
-(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES]; }
經過修改后,就可以使用CCTargetedTouchDelegate來處理觸摸事件了。
target方法中,有三個參數,其中第二個參數很重要,指的是優先級,比如你有兩個Layer都設置了相應觸摸事件,那么優先級高的,會先響應觸摸事件。(數值越低表示優先級越高)
關于swallowsTouches參數
Cocos2d用CCTouchDispatcher類分發從IOS獲取的觸摸事件。它有兩種分發方式一種是CCStandardTouchDelegate,一種是CCTargetedTouchDelegate。前者將所有的Touch信息直接傳遞給實現該協議的對象,而后者則要求實現該代理的類一次只能處理一個事件,并且吃掉該事件,不再傳播(swallowTouches=YES;)。
所以CCTargetedTouchDelegate這個協議針對單點觸控,而CCStandardTouchDelegate則可以處理當前屏幕觸摸的每個觸摸點,好像最多是十個吧。CCLayer默認情況下如果設定接受Touch事件,是使用標準協議。
我們可以看到,在注冊協議的時候分別對應這樣的方式:
-(void) addStandardDelegate:(id<CCStandardTouchDelegate>) delegate priority:(int)priority;
-(void) addTargetedDelegate:(id<CCTargetedTouchDelegate>) delegate priority:(int)priority swallowsTouches:(BOOL)swallowsTouches;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。