首页 > 代码库 > cocos2d-x3.0 PageView

cocos2d-x3.0 PageView


.h加入例如以下代码:

    void pageViewEvent(Ref *pSender, PageViewEventType type);


.m

   layout = Layout::create();
        layout->setSize(Size(widgetSize.width, widgetSize.height));
       
        //横向排列,这里相似Android里的线性布局
        //layout->setLayoutType(LAYOUT_RELATIVE);
        /*以图片为背景*/
        layout->setBackGroundImageScale9Enabled(true);
        layout->setBackGroundImage("green_edit.png");
        
        layout->setPosition(Point(0,0));
        addChild(layout);

        /******************************************************************************************/
        PageView* pageView = PageView::create();
        pageView->setSize(Size(480, 260));
        pageView->setPosition(Point(widgetSize.width / 2, widgetSize.height / 2));
        
        for (int i = 0; i < 3; i++) {
            Layout* layout1 = Layout::create();
            layout1->setSize(Size(480, 260));
            
            ImageView* imageView = ImageView::create("scrollviewbg.png");
            imageView->setScale9Enabled(true);
            imageView->setSize(Size(480, 260));
            imageView->setPosition(Point(layout1->getSize().width / 2, layout1->getSize().height / 2));
            layout1->addChild(imageView);
            
            
            
            Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30);
            label->setColor(Color3B(92, 192, 192));
            label->setPosition(Point(layout1->getSize().width / 2.0f, layout1->getSize().height / 2.0f));
            layout1->addChild(label);
            
            
            
            pageView->addPage(layout1);
            
            
            

        }
       
        pageView->addEventListenerPageView(this, pagevieweventselector(LayoutTest::pageViewEvent));
        
        layout->addChild(pageView);

void LayoutTest::pageViewEvent(cocos2d::Ref *pSender, PageViewEventType type)
{
    switch (type) {
        case cocos2d::ui::PAGEVIEW_EVENT_TURNING:
        {
            PageView* pageView = dynamic_cast<PageView*>(pSender);
            log("%d",pageView->getCurPageIndex() + 1);
        }
            break;
            
        
        default:
            break;
    }
}
技术分享

cocos2d-x3.0 PageView