新手CrossApp 之CASegmentControl小结

分段按钮,segmentedControl由一个或多个segment组成,点击不同段得到不同的信息,每次只能选择一段。

        CASegmentedControl* defaultCASegmentControl = CASegmentedControl::createWithCenter(CCRect(size.width*0.5, size.height*0.15, size.width*0.8, size.height*0.1), 3);//创建一个3段按钮
defaultCASegmentControl->setSelectedAtIndex(1);//默认选中第一段

        this->getView()->addSubview(defaultCASegmentControl);



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

               //循环3次,设置0-2个按钮的3种按钮背景图片,
sprintf(segNormal, "source_material/seg_normal%d.png",i+1);
sprintf(segHighlighted, "source_material/seg_highlighted%d.png", i+1);
sprintf(segSelect, "source_material/seg_selected%d.png", i+1);
sprintf(Title, "number%d", i);//设置标题

segmented->setTitleAtIndex(Title,i,CAControlStateAll);//把字体添加进全状态(正常,高亮,选中)按钮中
segmented->setTitleFontName("fonts/Marker Felt"); //设置字体类型
segmented->setBackgroundImageAtIndex(CAImage::create(segNormal),i,CAControlStateNormal);

                      //设置0,1,2个按钮正常时候的背景图片
segmented->setBackgroundViewAtIndex(CAScale9ImageView::createWithImage(CAImage::create(segHighlighted)), i, CAControlStateHighlighted);
segmented->setBackgroundImageAtIndex(CAImage::create(segSelect), i, CAControlStateSelected);
}
segmented->setTitleColorAtIndex(CAColor_white, 0, CAControlStateAll);//设置0号按钮的字体颜色
segmented->setTitleColorAtIndex(CAColor_gray, 1, CAControlStateAll);
segmented->setTitleColorAtIndex(CAColor_yellow, 2, CAControlStateAll);

        segmented->addTarget(this,CAControl_selector(FirstViewController::segmentCallback));//添加事件


void FirstViewController::segmentCallback(CAControl* btn, CCPoint point){
    //CAControl下的子类的事件回调函数必须有这两个类型形参

switch (segmented->getSelectedIndex())
{
case 0: defaultPage->setText(UTF8("默认样式")); break;
case 1: defaultPage->setText("The second page"); break;
case 2: defaultPage->setText("The third page"); break;
default: 
break;
}
}

新手CrossApp 之CASegmentControl小结_第1张图片

你可能感兴趣的:(CrossApp)