注册

[寒江孤叶丶的CrossApp之旅_08][入门系列]CASegmentedControl的简...

CASegmentedControl的简单介绍

CASegmentedControl相当于Android中的TabView。在开发过程中十分常用。

CASegmentedControl使用起来并不负责,寥寥几行代码便可搞定。

segment = CASegmentedControl::createWithCenter(CADipRect(size.width*0.5,size.height*0.3, size.width*0.8, size.height*0.1), 3);
segment->setTitleAtIndex("Page0", 0, CAControlStateAll);//根据index设置标题文字
segment->setTitleAtIndex("Page1", 1, CAControlStateAll);
segment->setTitleAtIndex("Page2", 2, CAControlStateAll);
segment->setTitleColorAtIndex(CAColor_white, 0, CAControlStateAll);
//根据index设置颜色
segment->setTitleColorAtIndex(CAColor_gray, 1, CAControlStateAll);
segment->setTitleColorAtIndex(CAColor_yellow, 2, CAControlStateAll);
segment->setSelectedAtIndex(1);
//设置当前选中项
segment->addTarget(this, CAControl_selector(MainMenuViewController::segmentCallback));//设置点击回调函数
this->getView()->addSubview(segment); 并设置一个回调函数

void MainMenuViewController::segmentCallback(CAControl* btn, CCPoint point)
{
switch (segment->getselectedIndex())
{
case 0:
{
label->setText("The first page");//替换为相关**作即可
break;
}
case 1:
{
label->setText("The second page");
break;
}
case 2:
{
label->setText("The third page");
break;
}
default:
break;
}
} 其中有一点需要说明,如果在会掉函数中 通过setTag 和getSubViewByTag的方式获得 View,会出现空指针的问题,(不知道是不是我的方法不对,希望高手解答),所以在此我的方法是将segment和label都设置为类的成员函数。
CASegmentedControl可以设置背景图案,方法如下:

char segNormal = "";
char segHighlighted = "";
char segSelected = "";
char pages = "";

segment = CASegmentedControl::createWithCenter(CADipRect(size.width*0.5,size.height*0.3, size.width*0.8, size.height*0.1), 3);
for (int i = 0; i < 3; i++)
{

sprintf(segNormal,"source_material/seg_normal%d.png",i+1);
sprintf(segHighlighted,"source_material/seg_highlighted%d.png",i+1);
sprintf(segSelected,"source_material/seg_selected%d.png",i+1);
sprintf(pages, "Number%d", i + 1);
segment->setTitleAtIndex(pages, i, CAControlStateAll);
segment->setBackgroundImageAtIndex(CAImage::create(segNormal), i, CAControlStateNormal);
segment->setBackgroundImageAtIndex(CAImage::create(segHighlighted), i, CAControlStateHighlighted);
segment->setBackgroundImageAtIndex(CAImage::create(segSelected), i, CAControlStateSelected);
}

segment->setTitleColorAtIndex(CAColor_white, 0, CAControlStateAll);
//根据index设置颜色
segment->setTitleColorAtIndex(CAColor_gray, 1, CAControlStateAll);
segment->setTitleColorAtIndex(CAColor_yellow, 2, CAControlStateAll);
segment->setSelectedAtIndex(1);
//设置当前选中项
segment->addTarget(this, CAControl_selector(MainMenuViewController::segmentCallback));//设置点击回调函数
this->getView()->addSubview(segment); 在CASegmentedControl的类中有两个有关开始点击,和取消点击的虚函数,如果需要设置相关监听,则需要对CASegmentedControl类进行集成,然后重写这两个函数,这两个函数分别是:
virtual void onExitTransitionDidStart();
virtual void onEnterTransitionDidFinish();



已邀请:

要回复问题请先登录注册