crossApp初级-CAButton类-7

类说明
按钮类,主要为了接收用户的点击操作,从而触发特定的事件 。已经定义好类对象可以存在的样式,我们不需要自己去定义按钮的样式 。(实际上也限制了按钮的样式)
基类
属性
访问修饰符 属性名 说明
背景视图
Image 不同状态显示的图片
Type 的类型
Title 的标题
image的颜色
标题颜色
标题的字体
方法
访问修饰符 方法名 说明
设置的状态
判断文本标签是否相等(此方法在6.0版本已经
被废弃)
中断按钮回调事件
一 。包括三种类型,采用第一种创建后看不见任何明显效果,需要自己设置;第二种,默认为创建的添加了矩形的外边框;第三种,默认为创建的添加圆角的外边框 。使用,, 。
typedef enum{CAButtonTypeCustom = 0,CAButtonTypeSquareRect,CAButtonTypeRoundedRect,} CAButtonType;
代码示例:
FirstViewController::viewDidLoad(){// 设置为CAButtonTypeCustom类型,看不见Button;// CAButton* cabutton1 = CAButton::create(CAButtonTypeCustom);// 设置为长方形的buttonCAButton* cabutton1 = CAButton::create(CAButtonTypeSquareRect);cabutton1->setCenter(CCRect(240, 320, 100, 0));this->getView()->addSubview(cabutton1);}//效果如下:

crossApp初级-CAButton类-7

文章插图
FirstViewController::viewDidLoad(){// 设置为CAButtonTypeCustom类型,看不见Button;// CAButton* cabutton1 = CAButton::create(CAButtonTypeCustom);// 设置为边角圆滑的长方形的buttonCAButton* cabutton1 = CAButton::create(CAButtonTypeRoundedRect);cabutton1->setCenter(CCRect(240, 320, 100, 0));this->getView()->addSubview(cabutton1);}//效果如下图:
crossApp初级-CAButton类-7

文章插图
二 。设置上的文字:使用e()函数 。
crossApp初级-CAButton类-7

文章插图
void FirstViewController::viewDidLoad(){// 设置为CAButtonTypeCustom类型,看不见Button;但是看的见button上的文字CAButton* cabutton1 = CAButton::create(CAButtonTypeCustom);cabutton1->setCenter(CCRect(100, 100, 100, 40));//设置 Button 名字的题目(名字)cabutton1->setTitleForState(CAControlStateNormal, "startGame");//设置Button 的状态颜色,当按下的时候会变成设置的红色cabutton1->setTitleColorForState(CAControlStateHighlighted, ccc4(255, 0, 0, 255));this->getView()->addSubview(cabutton1);}//效果如下图:
crossApp初级-CAButton类-7

文章插图

其他的2中形状效果如下:
crossApp初级-CAButton类-7

文章插图
注意这里 的长度太小,文字显示不全
void FirstViewController::viewDidLoad(){// 设置为CAButtonTypeCustom类型,看不见Button;CAButton* cabutton1 = CAButton::create(CAButtonTypeSquareRect);cabutton1->setFrame(CCRect(100, 100, 200, 40));//设置 Button 名字的题目(名字)cabutton1->setTitleForState(CAControlStateNormal, "startGame");//设置Button 的状态颜色cabutton1->setTitleColorForState(CAControlStateHighlighted, ccc4(255, 0, 0, 255));this->getView()->addSubview(cabutton1);}效果如下:
crossApp初级-CAButton类-7

文章插图
ect 形状的效果如下:
crossApp初级-CAButton类-7

文章插图
三 。设置按钮背景的颜色和图片,9宫格,使用 State()函数
1.设置纯色背景
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,CAView::createWithColor(CAColor_blue));this->getView()->addSubview(cabutton1);}//效果如下: