您的位置:首页 > 移动开发 > Cocos引擎

cocos代码研究(24)Widget子类PageView学习笔记

2015-10-09 20:49 736 查看

理论基础

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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: