CAButton(按钮)

2018-08-29 16:10 更新

类说明

在app开放中按钮是最常用的控件之一,大家对Button的需要也多种多样,CrossApp提供了CAButton,其使用思路主要是根据状态设置来完成的。

CAButton按钮类,主要为了接收用户的点击操作,从而触发特定的事件。


CAButton 属性 (点击属性名可查看属性介绍)

属性说明
AllowsSelected是否可以选择
Selected是否被选择
TouchClick是否被触摸点击


CAButton 方法 (点击属性名可查看属性介绍)

方法说明
create创建
createWithFrame创建,并指定其Frame
createWithCenter创建,并指定其Center
init初始化
setBackGroundViewForState设置Button的背景View
getBackGroundViewForState获取Button的背景View
setImageForState设置Button的图片(不支持九宫格)
getImageForState获取Button的图片(不支持九宫格)
setTitleForState设置Button标题的显示文本
getTitleForState获取Button标题的显示文本
setImageColorForState设置Button的图像颜色和状态
setTitleColorForState设置Button的标题颜色和状态
setTitleFontName设置Button文本的字体
setImageOffset设置图像偏移量
setImageSize设置图像大小
setTitleOffset设置标题偏移量
setTitleLabelSize设置标题标签大小
setTitleFontSize设置标题字体大小
setControlState设置状态
interruptTouchState中断接触状态
ccTouchBegan触摸事件开始时的回调函数
ccTouchMoved触摸事件中触点移动时的回调函数
ccTouchEnded触摸事件结束时的回调函数
ccTouchCancelled触摸非正常结束时的回调函数。(例如:电话或锁屏)
addTarget添加回调事件
removeTarget删除回调事件
removeAllTargets删除所有回调事件


/*
    创建一个Button
    参数类型CAButtonType:CAButtonTypeCustom、CAButtonTypeSquareRect、CAButtonTypeRoundedRect
    CAButtonTypeCustom:默认看不见任何效果,需要自己定义效果
    CAButtonTypeSquareRect:矩形边框类型
    CAButtonTypeRoundedRect:圆角边框类型
*/
CAButton* firstButton = CAButton::create(CAButtonTypeRoundedRect);
 
/*
    设置Button的状态显示文本
    CAControlStateNormal:缺省状态
    CAControlStateHighlighted:高亮状态
    CAControlStateDisabled:禁用状态
    CAControlStateSelected:选中状态
    CAControlStateAll:全部状态
*/
firstButton->setTitleForState(CAControlStateAll,"Button");
 
/*
    设置Button的状态显示文本的颜色
*/
firstButton->setTitleColorForState(CAControlStateAll, ccc4(0, 0, 0, 255));
 
//设置Button文本的字体
firstButton->setTitleFontName("宋体");
 
/*
    设置Button的状态的背景View
*/
//九宫格图
//firstButton->setBackGroundViewForState(CAControlStateAll,CAScale9ImageView::createWithImage("HelloWorld.png"));
 
//设置纯色View
//firstButton->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_red));
 
/*
    设置Button的状态的图片(不支持九宫格)
*/
//firstButton->setImageForState(CAControlStateAll,CAImage::create("HelloWorld.png"));
 
/*
    设置Button的状态
*/
//firstButton->setControlState(CAControlStateHighlighted);
 
/*
    设置Frame(如果不设置Frame,默认是不显示的)
*/
firstButton->setFrame(DRect(100,100,200,80));
 
//添加到绘制
this->getView()->addSubview(firstButton);
 
/*
    设置Button的监听
    CAControlEventTouchDown:按下按钮响应
    CAControlEventTouchDownRepeat:(未实现预留)双击时响应
    CAControlEventTouchMoved:触点在Button范围内移动
    CAControlEventTouchMovedOutSide:触点移动到Button范围外
    CAControlEventTouchUpInSide:触点在Button范围内抬起
    CAControlEventTouchUpSide:Button抬起
    CAControlEventTouchValueChanged:此状态在CAButton无效,在CASlider中响应
*/
firstButton->addTarget(this, CAControl_selector(FirstViewController::callbackDown), CAControlEventTouchDown);
 
//此状态6.0版本未实现
//firstButton->addTarget(this, CAControl_selector(FirstViewController::callbackDownRepeat), CAControlEventTouchDownRepeat);
 
firstButton->addTarget(this, CAControl_selector(FirstViewController::callbackMoved), CAControlEventTouchMoved);
 
firstButton->addTarget(this, CAControl_selector(FirstViewController::callbackMovedOutSide), CAControlEventTouchMovedOutSide);
 
firstButton->addTarget(this, CAControl_selector(FirstViewController::callbackUpInSide), CAControlEventTouchUpInSide);
 
firstButton->addTarget(this, CAControl_selector(FirstViewController::callbackUpSide), CAControlEventTouchUpSide);
 
//此状态6.0版本 Button无效
//firstButton->addTarget(this, CAControl_selector(FirstViewController::callbackValueChanged), CAControlEventTouchValueChanged);
 
/*
    中断监听
*/
//firstButton->interruptTouchState();

监听的回调函数也很简单:有两个参数:CAControl* 和DPoint

CAControl是Button本身

DPoint是触点的坐标

void FirstViewController::callbackDown(CAControl* control, DPoint point)
{
        CCLog("callbackDown()-->");
}
 
void FirstViewController::callbackDownRepeat(CAControl* control, DPoint point)
{
        CCLog("callbackDownRepeat()-->");
}
 
void FirstViewController::callbackMoved(CAControl* control, DPoint point)
{
        CCLog("callbackMoved()-->");
}
 
void FirstViewController::callbackMovedOutSide(CAControl* control, DPoint point)
{
        CCLog("callbackMovedOutSide()-->");
}
 
void FirstViewController::callbackUpInSide(CAControl* control, DPoint point)
{
        CCLog("callbackUpInSide()-->");
        //((CAButton*)control)->setTitleForState(CAControlStateAll, "changed");
}
 
void FirstViewController::callbackUpSide(CAControl* control, DPoint point)
{
        CCLog("callbackUpSide()-->");
}
 
void FirstViewController::callbackValueChanged(CAControl* control, DPoint point)
{
        CCLog("callbackValueChanged()-->");
}

CAButton 属性说明

AllowsSelected

类型:bool

解释:是否可以选择。is/set{}。


Selected

类型:bool

解释:是否被选择。is{}。


TouchClick

类型:bool

解释:是否被触摸点击。is{}。


CAButton 方法说明

static CAButton* create(const CAButtonType& buttonType);

返回值:static CAButton*

参数:

类型
参数名说明
const CAButtonType&buttonType按钮类型

解释:创建

typedef enum
{
    CAButtonTypeCustom = 0,     默认看不见任何效果,需要自己定义效果
    CAButtonTypeSquareRect,     矩形边框类型
    CAButtonTypeRoundedRect,    圆角边框类型
} CAButtonType;

static CAButton* createWithFrame(const DRect& rect, const CAButtonType& buttonType);

返回值:static CAButton*

参数:

类型
参数名说明
const DRect&rect区域大小
 const CAButtonType&buttonType按钮类型

解释:创建,并指定其Frame

typedef enum
{
    CAButtonTypeCustom = 0,     默认看不见任何效果,需要自己定义效果
    CAButtonTypeSquareRect,     矩形边框类型
    CAButtonTypeRoundedRect,    圆角边框类型
} CAButtonType;


static CAButton* createWithCenter(const DRect& rect, const CAButtonType& buttonType);

返回值:static CAButton*

参数:

类型
参数名说明
const DRect&rect中心点的位置及大小
 const CAButtonType&buttonType按钮类型

解释:创建,并指定其Center

typedef enum
{
    CAButtonTypeCustom = 0,     默认看不见任何效果,需要自己定义效果
    CAButtonTypeSquareRect,     矩形边框类型
    CAButtonTypeRoundedRect,    圆角边框类型
} CAButtonType;


virtual bool init();

返回值:virtual bool

参数:

解释:初始化


void setBackGroundViewForState(const CAControlState& controlState, CAView *var);

返回值:void

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态
CAView*var图像

解释:设置Button的状态的背景View

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;


CAView* getBackGroundViewForState(const CAControlState& controlState);

返回值:CAView*

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态

解释:获取Button的状态的背景View

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

void setImageForState(const CAControlState& controlState, CAImage* var);

返回值:void

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态
CAImage*var图像

解释:设置Button的状态和图像(不支持九宫格)

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

CAImage* getImageForState(const CAControlState& controlState);

返回值:CAImage*

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态

解释:获取Button的状态和图像(不支持九宫格)

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

void setTitleForState(const CAControlState& controlState, const std::string& var);

返回值:void

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态
const std::string&var文本

解释:设置Button标题的显示文本

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

const std::string& getTitleForState(const CAControlState& controlState);

返回值:const std::string&

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态

解释:获取Button标题的显示文本

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

void setImageColorForState(const CAControlState& controlState, const CAColor4B& var);

返回值:void

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态
const CAColor4B&var颜色

解释:设置Button的图像颜色和状态

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

void setTitleColorForState(const CAControlState& controlState, const CAColor4B& var);

返回值:void

参数:

类型
参数名说明
const CAControlState&controlStateButton的状态
const CAColor4B&var颜色

解释:设置Button的标题颜色和状态

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

void setTitleFontName(const std::string& var);

返回值:void

参数:

类型
参数名说明
const std::string&var文本

解释:设置Button文本的字体


void setImageOffset(const DSize& offset);

返回值:void

参数:

类型
参数名说明
const DSize&offset偏移量

解释:设置图像偏移


void setImageSize(const DSize& size);

返回值:void

参数:

类型
参数名说明
const DSize&size大小

解释:设置图像大小


void setTitleOffset(const DSize& offset);

返回值:void

参数:

类型
参数名说明
const DSize&offset偏移量

解释:设置标题偏移量


void setTitleLabelSize(const DSize& size);

返回值:void

参数:

类型
参数名说明
const DSize&size大小

解释:设置标题标签大小


void setTitleFontSize(float fontSize);

返回值:void

参数:

类型
参数名说明
floatfontSize字体大小

解释:设置标题字体大小


virtual void setControlState(const CAControlState& var);

返回值:virtual void

参数:

类型
参数名说明
const CAControlState&var状态

解释:设置状态

typedef enum
{
    CAControlStateNormal:       缺省状态
    CAControlStateHighlighted:  高亮状态
    CAControlStateDisabled:     禁用状态
    CAControlStateSelected:     选中状态
    CAControlStateAll:          全部状态
}CAControlState;

void interruptTouchState();

返回值:void

参数:

解释:中断接触状态


virtual bool ccTouchBegan(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual bool

参数:

类型
参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEven此参数待定

解释:触摸事件开始时的回调函数


virtual void ccTouchMoved(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual void

参数:

类型
参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEven此参数待定

解释:触摸事件中触点移动时的回调函数


virtual void ccTouchEnded(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual void

参数:

类型
参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEven此参数待定

解释:触摸事件结束时的回调函数


virtual void ccTouchCancelled(CATouch *pTouch, CAEvent *pEvent);

返回值:virtual void

参数:

类型
参数名说明
CATouch*pTouch触摸传递对象
CAEvent*pEven此参数待定

解释:触摸非正常结束时的回调函数。(例如:电话或锁屏)


using CAControl::addTarget;

返回值:using

参数:

解释:添加回调事件


using CAControl::removeTarget;

返回值:using

参数:

解释:删除回调事件


using CAControl::removeAllTargets;

返回值:using

参数:

解释:删除所有回调事件


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号