PageView的使用(这里实现的功能是有三页,每页有三个图片精灵,每个图片精灵各加了一个按钮)
//创建一个pageView对象
auto pageView = PageView::create();
pageView->setContentSize(Size(960,480));
pageView->setPosition(Vec2((_screenWidth - pageView->getContentSize().width)/2,(_screenHeight - pageView->getContentSize().height)/2));//左下角坐标位置
//创建三个layout(关卡图片)//设置页数
for (int i = 1; i <= 3;i++)//有三页
{
//创建一个layout
Layout *lo = Layout::create();
lo->setBackGroundImage("");
lo->setBackGroundImageScale9Enabled(true);
lo->setContentSize(Size(960,480));
for (int j = 1;j <= 3;j++)
{
//底图
auto imageView = ImageView::create(StringUtils::format("fang%d.png",j));
imageView->setPosition(Vec2(j*300 - 125,pageView->getContentSize().height/2));
lo->addChild(imageView);
//Button
auto btn = Button::create("Get-Gold_normal@2x.png");
btn->setTag(j);
btn->setPosition(Vec2(imageView->getContentSize().width/2,20));
btn->addTouchEventListener([=](Ref* pSender,Widget::TouchEventType type){
if (type == Widget::TouchEventType::ENDED)
{
if (btn->getTag() == 1)
{
log("first_btn1");
}
if (btn->getTag() == 2)
{
log("first_btn2");
}
if (btn->getTag() == 3)
{
log("first_btn3");
}
}
});
imageView->addChild(btn);
}
pageView->addPage(lo);
}
pageView->addEventListener([=](Ref* pSender,PageView::EventType type){
if (type == PageView::EventType::TURNING)
{
PageView* pageView = dynamic_cast<PageView*>(pSender);
// 在_displayValueLabel文本中显示PageIndex
_displayValueLabel->setString(StringUtils::format("page = %ld",pageView->getCurPageIndex() + 1));
}
});
this->addChild(pageView);