crossApp初级-CAButton类-7

类说明

按钮类,主要为了接收用户的点击操作,从而触发特定的事件。crossApp 已经定义好Button类对象可以存在的样式,我们不需要自己去定义按钮的样式。(实际上也限制了按钮的样式)

基类
CAControl
属性
访问修饰符                        属性名                        说明
public                        BackGroundView                背景视图
public                        Image                                不同状态显示的图片
public                        Type                                        button的类型
public                        Title                                        button的标题
public                        ImageColor                        image的颜色
public                        TitleColor                                标题颜色
public                        TitleFontName                        标题的字体
方法
访问修饰符                        方法名                        说明
public                        setControlState                        设置button的状态
public                        isTextTagEqual                        判断文本标签是否相等(此方法在6.0版本已经

被废弃)

public                        interruptTouchState                中断按钮回调事件

一。包括三种button类型,采用第一种button创建后看不见任何明显效果,需要自己设置;第二种button,默认为创建的button添加了矩形的外边框;第三种button,默认为创建的button添加圆角的外边框。使用create,createWithFrame,createWitchCenter。

typedef enum

{

      CAButtonTypeCustom = 0,

      CAButtonTypeSquareRect,

      CAButtonTypeRoundedRect,

} CAButtonType;

代码示例:

FirstViewController::viewDidLoad()
{
	
//	设置为CAButtonTypeCustom类型,看不见Button;
//	CAButton* cabutton1 = CAButton::create(CAButtonTypeCustom);
//	设置为长方形的button
	CAButton* cabutton1 = CAButton::create(CAButtonTypeSquareRect);
	cabutton1->setCenter(CCRect(240, 320, 100, 0));

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

}//效果如下:

FirstViewController::viewDidLoad()
{
	
//	设置为CAButtonTypeCustom类型,看不见Button;
//	CAButton* cabutton1 = CAButton::create(CAButtonTypeCustom);
//	设置为边角圆滑的长方形的button
	CAButton* cabutton1 = CAButton::create(CAButtonTypeRoundedRect);
	cabutton1->setCenter(CCRect(240, 320, 100, 0));

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

}//效果如下图:

二。设置button上的文字:使用 setTitleColorForState()函数。

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);

}//效果如下图:


 

其他的2中形状效果如下:

注意这里button 的长度太小,文字显示不全

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);

}效果如下:

CAButtonTypeRoundedRect 形状的效果如下:

三。设置按钮背景的颜色和图片,9宫格,使用 setBackGroundViewForState()函数

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);

}//效果如下:

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);

3.设置9宫格背景图片,使用 CAScale9ImageView()

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);

}

3.设置button 状态图片,不支持显示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);

}//当按下的时候,会出现图片,也可以设置其他的状态的图片

四。按钮的监听事件和响应

  /*
                Button的监听状态列表
                CAControlEventTouchDown:按下按钮响应
                CAControlEventTouchDownRepeat:(未实现预留)双击时响应
                CAControlEventTouchMoved:触点在Button范围内移动
                CAControlEventTouchMovedOutSide:触点移动到Button范围外
                CAControlEventTouchUpInSide:触点在Button范围内抬起
                CAControlEventTouchUpSide:Button抬起
                CAControlEventTouchValueChanged:此状态在CAButton无效,在CASlider中响应
        */

4.1 指定button对象的添加一个函数处理,addTarget()

	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);


}

void FirstViewController::bt1_down(CAControl* control, CCPoint point){

	CCLog("bt1 check down!!!");

}//效果如下:

它会在每次点击之后,在console 中输出 bt1 check down!!!

4.2 监听其他button的状态的处理

addTarget(this, CAControl_selector(FirstViewController::callbackMoved), CAControlEventTouchMoved);
addTarget(this, CAControl_selector(FirstViewController::callbackMovedOutSide), CAControlEventTouchMovedOutSide);

注意:

1.不设置button 的frame,button是不会显示的

        /*
                设置Frame(如果不设置Frame,默认是不显示的)
        */

2.button 的状态是可以设置的:

                设置Button的状态
       /*
        //firstButton->setControlState(CAControlStateHighlighted);
       */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值