crossApp初级-CAButton类-7( 二 )


crossApp初级-CAButton类-7

文章插图
2.设置背景图片
// 设置为CAButtonTypeCustom类型,看不见Button;CAButton* cabutton1 = CAButton::create(CAButtonTypeRoundedRect);cabutton1->setFrame(CCRect(100, 100, 200, 40));//设置 Button 的题目(名字)cabutton1->setTitleForState(CAControlStateNormal, "startGame");//设置Button 名字的状态颜色cabutton1->setTitleColorForState(CAControlStateHighlighted, ccc4(255, 0, 0, 255));//设置button图片背景cabutton1->setBackGroundViewForState(CAControlStateAll,CAImageView::createWithImage(CAImage::create("r/HelloWorld.png")));this->getView()->addSubview(cabutton1);
crossApp初级-CAButton类-7

文章插图
3.设置9宫格背景图片,使用 ()
void FirstViewController::viewDidLoad(){// 设置为CAButtonTypeCustom类型,看不见Button;CAButton* cabutton1 = CAButton::create(CAButtonTypeRoundedRect);cabutton1->setFrame(CCRect(100, 100, 200, 40));//设置 Button 的题目(名字)cabutton1->setTitleForState(CAControlStateNormal, "startGame");//设置Button 名字的状态颜色cabutton1->setTitleColorForState(CAControlStateHighlighted, ccc4(255, 0, 0, 255));//设置button图片背景cabutton1->setBackGroundViewForState(CAControlStateAll,CAScale9ImageView::createWithImage(CAImage::create("r/HelloWorld.png")));this->getView()->addSubview(cabutton1);}
crossApp初级-CAButton类-7

文章插图
3.设置 状态图片,不支持显示9宫格效果显示图片
void FirstViewController::viewDidLoad(){// 设置为CAButtonTypeCustom类型,看不见Button;CAButton* cabutton1 = CAButton::create(CAButtonTypeRoundedRect);cabutton1->setFrame(CCRect(100, 100, 200, 40));//设置 Button 的题目(名字)cabutton1->setTitleForState(CAControlStateNormal, "startGame");//设置Button 名字的状态颜色cabutton1->setTitleColorForState(CAControlStateHighlighted, ccc4(255, 0, 0, 255));//设置状态图片cabutton1->setImageForState(CAControlStateHighlighted, CAImage::create("r/vdo_play_down.png"));this->getView()->addSubview(cabutton1);}//当按下的时候,会出现图片,也可以设置其他的状态的图片
crossApp初级-CAButton类-7

文章插图
四 。按钮的监听事件和响应
/*
的监听状态列表
own:按下按钮响应
crossApp初级-CAButton类-7

文章插图
:(未实现预留)双击时响应
oved:触点在范围内移动
:触点移动到范围外
:触点在范围内抬起
pSide:抬起
:此状态在无效,在中响应
*/
4.1 指定对象的添加一个函数处理,()
cabutton1->addTarget(this, CAControl_selector(FirstViewController::bt1_down), CAControlEventTouchDown);
注意:要在.h 文件中申明这个函数
实例如下:
void FirstViewController::viewDidLoad(){// 设置为CAButtonTypeCustom类型,看不见Button;CAButton* cabutton1 = CAButton::create(CAButtonTypeCustom);cabutton1->setFrame(CCRect(100, 100, 200, 40));//设置 Button 的题目(名字)cabutton1->setTitleForState(CAControlStateNormal, "startgame");cabutton1->setTitleForState(CAControlStateHighlighted, "STARTGAME");//设置Button 名字的状态颜色cabutton1->setTitleColorForState(CAControlStateHighlighted, ccc4(255, 0, 0, 255));//设置状态图片cabutton1->setImageForState(CAControlStateHighlighted, CAImage::create("r/vdo_play_down.png"));cabutton1->addTarget(this, CAControl_selector(FirstViewController::bt1_down), CAControlEventTouchDown);this->getView()->addSubview(cabutton1);}