CCTableView 與 CCMenu巧妙結合

來源:互聯網
上載者:User

今天被賦予了一項新的任務,那就是看看cocos2d新版本的CCTableView能不能解決項目TableView的Bug。

項目內的TableView會存在如下bug:

  • 當TableView裡面的MenuItem滾動出View方框時,使用者應該不能在View方框外選中該MenuItem的。但使用者卻能點擊到。
  • 當TableView上面還有一層Layer時,當點擊Layer的時候,TableView裡面的MenuITem也會被選中。
第一個bug的主要原因是因為CCMenu的優先順序太高(-128),另外一點就是即使當CCMenu優先順序低了,也很難讓二者行為與一般的視窗空間相同,因為他們之間沒有正常的視窗父子關係。cocos2d的訊息處理是基於優先順序,也就是說即使被覆蓋的元素,也能第一個處理訊息,這與經典視窗的訊息處理流程相悖。所以我通過讓CCMenu與CCTableView通過建立視窗父子關係來達到解決第一個bug的目的。
  1. 首先派生出CCMenuNoMessage自CCMenu,並重載registerTouchDispatcher(),並上此函數為空白,這樣這個菜單一實例是不會接受到任何訊息的,因為我們的目的是建立父子視窗關係,所以應該完全有父視窗決定訊息傳遞。並添加靜態成員函數static CCMenuNoMessage * create(CCMenuItem* item, ...);在此函數的實現中,拷貝CCMenu內對應的代碼,但需要改變裡面的new()對象,CCMenuNoMessage *pRet = new CCMenuNoMessage();
  2. 再派生出CCTableViewWindow自CCTableView,重載所有的ccTouch···訊息處理函數,在ccTouchBegan中添加表單點擊處理,
    //if not hit bounding box    if( false == boundingBox().containsPoint( pt ) )    {        return false;    }

    這樣未點中TableView的訊息是不處理。

  3. 接著將訊息傳遞給所有CCMenu子視窗。
    //hande message to menu items    m_bBeganForMenu = true;    for( int i = 0; i < m_pCellsUsed->count(); ++i )    {        CCTableViewCell* pCell = ( CCTableViewCell* )m_pCellsUsed->objectAtIndex( i );        if( pCell->getChildrenCount() > 0 )        {            int iNumChildren = pCell->getChildren()->count();            for( int iChildIndex = 0; iChildIndex < iNumChildren; ++iChildIndex )            {                CCLayer* pItem = dynamic_cast< CCLayer* >( pCell->getChildren()->objectAtIndex( iChildIndex ) );                                //if not a layer, it won't process touch messages.                if( !pItem )                {                    continue;                }                                //if this menu item is interested in this message, add it to array for other messages                if( true == pItem->ccTouchBegan( touch, event ) )                {                    m_pMenuItemsInterest->addObject( pItem );                }            }        }    }        CCScrollView::ccTouchBegan( touch, event);

    通過標記變數來判斷,此次使用者點擊是否要滑動(後面訊息會用到),並將對點擊訊息感興趣的CCMenu保留下來,讓其接受ccTouchMove,ccTouchEnded訊息。

  4. 再處理ccTouchMoved訊息,
    //if this is the first ccTouchMove() message for menu itmes    if( m_bBeganForMenu )    {        //prevent menu items processing other ccTouchMove() messages        m_bBeganForMenu = false;        for( int i = 0; i < m_pMenuItemsInterest->count(); ++i )        {            CCLayer* pItem = ( CCLayer* )m_pMenuItemsInterest->objectAtIndex( i );            //menu items doesn't process ccTouchMove(), cancel it.            assert( NULL != pItem );            pItem->ccTouchCancelled( touch, event );        }                m_pMenuItemsInterest->removeAllObjects();    }            CCScrollView::ccTouchMoved(touch, event);

    如果ccTouchBegan之後緊接著是ccTouchMove訊息,那麼證明使用者要滑動,此時,我們讓對訊息感興趣的CCMenu釋放掉。注意這裡調用ccTouchCancelled訊息,而非ccTouchEnded,因為後者會觸發點擊訊息,這是我們不想看到的。

  5. 再處理ccTouchEnded訊息,
    //if ccTouchEnded message is the next message after ccTouchBegan() message processed,    //it proves that user wants to select this menu item.    //then, let some menu item process this message.    if( m_bBeganForMenu )    {        m_bBeganForMenu = false;        for( int i = 0; i < m_pMenuItemsInterest->count(); ++i )        {            CCLayer* pItem = ( CCLayer* )m_pMenuItemsInterest->objectAtIndex( i );            assert( NULL != pItem );            pItem->ccTouchEnded( touch, event );        }                m_pMenuItemsInterest->removeAllObjects();    }        CCScrollView::ccTouchEnded(touch, event);

    如果標記為真,證明使用者的觸摸未曾移動過就鬆開了,證明使用者想點擊次CCMenu,那麼我們應該讓對訊息感興趣的CCMenu完成點擊訊息。

通過這幾步,我們建立起來了一個正常的父子關係,能讓CCTableViewWindow與CCMenuNoMessage相處融洽,且解決了第一個bug。但第二個bug也可以稱為非bug,或者系統層級的bug,這是由cocos2d的訊息架構造成的,我們的CCTableViewWindow實在是勢單力薄,無法在一個不融洽的體系內解決這個bug。所以CCTableViewWindow已經完成了它的使命,如果要解決第二個bug,我初步想法是自己建立一套層級訊息管理器,讓CCLayer能根據ZOrder順序來處理訊息。目前這個工作還沒有讓我做,我暫時只是個想法。

第二個bug的解決方案已經給出,詳情將看cocos2d-x 建立自己的層級視窗訊息機制。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.