理論基礎
PageView類又稱Layout的管理器,可以讓用戶在多個Layout之間左右或者上下切換顯示,繼承自 Layout 。
代碼實踐
static PageView * create ()
創建一個空的PageView。
void addWidgetToPage (Widget *widget, ssize_t pageIdx, bool forceCreate)
把一個widget添加到指定的PageView頁中。
void addPage (Layout *page)
往PageView的最后插入一頁。
void insertPage (Layout *page, int idx)
在指定位置插入一頁。
void removePage (Layout *page)
從PageView中移除一頁。
void removePageAtIndex (ssize_t index)
移除指定位置的頁。
void setDirection (Direction direction)
變更容器翻頁方向。
Direction getDirection () const
獲取容器翻頁方向。
void removeAllPages ()
移除PageView中所有頁。
void scrollToPage (ssize_t idx)
滾動到一個指定的頁。
ssize_t getCurPageIndex () const
獲取當前顯示頁的索引號。
void setCurPageIndex (ssize_t index)
直接跳轉至指定頁面(非滾動)
Vector< Layout * > & getPages ()
獲取所有頁的列表。
Layout * getPage (ssize_t index)
獲取指定的頁。
void addEventListenerPageView (Ref *target, SEL_PageViewEvent selector)
添加一個頁面切換時的回調函數,當頁面切換時被調用。
void addEventListener (const ccPageViewCallback &callback)
添加一個頁面切換時的回調函數,當頁面切換時被調用。
void setCustomScrollThreshold (float threshold)
如果沒有指定該值,pageView會在滾到頁面一半時切換到下一頁。
float getCustomScrollThreshold () const
請求設定的切換頁面門限值。
void setUsingCustomScrollThreshold (bool flag)
是否使用用戶設置的切換頁面門限值。 如果設為false,那么pageView會在滾到頁面一半時切換到下一頁。
bool isUsingCustomScrollThreshold () const
請求是否使用用戶自定義頁面切換門限值。
實例:
// Create the page view PageView* pageView = PageView::create(); pageView->setContentSize(Size(240.0f, 100.0f)); Size backgroundSize = background->getContentSize(); pageView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + (backgroundSize.width - pageView->getContentSize().width) / 2.0f, (widgetSize.height - backgroundSize.height) / 2.0f + (backgroundSize.height - pageView->getContentSize().height) / 2.0f + 20)); int pageCount = 4; for (int i = 0; i < pageCount; ++i) { Layout* layout = Layout::create(); layout->setContentSize(Size(240.0f, 130.0f)); ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png"); imageView->setScale9Enabled(true); imageView->setContentSize(Size(240, 130)); imageView->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(imageView); Text* label = Text::create(StringUtils::format("page %d",(i+1)), "fonts/Marker Felt.ttf", 30); label->setColor(Color3B(192, 192, 192)); label->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f)); layout->addChild(label); pageView->insertPage(layout,i); } _uiLayer->addChild(pageView); pageView->setName("pageView");
