怎樣在 Cocos2d裡面使用 UITapGestureRecognizer來處理單擊和雙擊

來源:互聯網
上載者:User
              怎樣在 Cocos2d裡面使用 UITapGestureRecognizer來處理單擊和雙擊
            在CCLayer的標頭檔中定義:        UITapGestureRecognizer *singleTap;
       .m檔案中:       
        //處理  單擊        singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:                     @selector(handleSingleTap:)];                singleTap.numberOfTapsRequired=1;        singleTap.numberOfTouchesRequired=1;                [[[CCDirector  sharedDirector] openGLView] addGestureRecognizer: singleTap];        [singleTap release];


    如果 要處理雙擊:    

        doubleTap.numberOfTapsRequired = 2;        doubleTap.numberOfTouchesRequired = 1;

   下面是 響應函數:   

-(void)  handleSingleTap:(id)sender;

-(void)  handleSingleTap:(id)sender{    UNUSED(sender);    NSLog(@"handleSingleTap  \r\n");        CGSize screenSize = [CCDirector sharedDirector].winSize;     CGPoint  point=[singleTap locationInView:[[CCDirector  sharedDirector] openGLView]];CGPoint  glLocation=[[CCDirector sharedDirector] convertToGL:point];        //判斷點擊的是否是“想要的”的地區    bool  bSingleClickTableForme=false;    CGRect   tableFormeRect=CGRectMake(5.0f, 10.0f, 125.0f, 58.0f);    if ( CGRectContainsPoint(tableFormeRect, glLocation)==true)     {        bSingleClickTableForme=true;    }}

需要注意的是,如果給一個Layer加上了Singletap的手勢識別後,將會導致介面上面的其他組件比如:CCMenu不再能響應點擊,因為點擊訊息全部被手勢所截獲,所以一般不要使用SingleTap,但是使用DoubleTap是沒有什麼問題的。一定要實作類別似SingleTap的類似效果,可以處理TouchEnd事件,然後在裡面判斷座標位置來處理。


        

       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.