原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的CrossApp之旅系列]
博客地址:http://blog.csdn.net/qq446569365
CAButton在CrossApp的应用程序开发过程中,使用的非常频繁,很多控件也是以他为基础的,比如之前介绍的CAAlertView。
CAButton有四种创建方法:
CAButton::create( const CAButtonType &buttonType)
CAButton::createWithCenter(const CrossApp::CCRect &rect, const CAButtonType &buttonType)
CAButton::createWithColor(const CAColor4B &color4B)
CAButton::createWithFrame(const CrossApp::CCRect &rect, const CAButtonType &buttonType)
以上的四种方法相信大家很容易理解,唯一需要解释的便是CAButtonType,
CAButtonType是一个枚举类型,他共有3中形式,
typedef enum
{
CAButtonTypeCustom = 0,
CAButtonTypeSquareRect,
CAButtonTypeRoundedRect,
} CAButtonType;
CAButtonTypeCustom类型所创建的是一个没有边框的按钮,
CAButtonTypeSquareRect类型所创建的是一个带有矩形边框的按钮,
CAButtonTypeRoundedRect类型所创建的是一个带有圆角矩形边框的按钮,
CAButton的成员方法并不是很多。下面对其进行简单的介绍:void setBackGroundViewForState(constCAControlState& controlState, CAView *var);
为不同状态的按钮设置背景图案
CAView* getBackGroundViewForState(constCAControlState& controlState);
获取不同状态按钮的背景图案
void setImageForState(constCAControlState& controlState, CAImage* var);
为不同状态的按钮设置图案
void setTitleForState(constCAControlState& controlState, conststd::string& var);
为按钮设置标题文字
void setImageColorForState(constCAControlState& controlState, constCAColor4B& var);
为不同状态按钮设置颜色
void setTitleColorForState(constCAControlState& controlState, constCAColor4B& var);
为不同状态按钮设置标题颜色
void setTitleFontName(conststd::string& var);
设置按钮标题字体
virtual void setControlState(constCAControlState& var);
设置按钮当前状态
using CAControl::addTarget;
设置事件回调CAButton的状态共有4种:
typedef enum
{
CAControlStateNormal = 0,
CAControlStateHighlighted,
CAControlStateDisabled,
CAControlStateSelected,
CAControlStateAll
}CAControlState;
CAControlStateNormal 一般状态
CAControlStateHighlighted 高亮状态
CAControlStateDisabled 禁用状态
CAControlStateSelected 选中状态
CAControlStateAll 以上四种状态合到一起
附上一段使用代码auto btn = CAButton::create(CAButtonTypeRoundedRect);
btn->setCenter(CADipRect(size.width*0.5 + 50, size.height*0.5, size.width*0.25, size.height*0.1));
btn->setTag(100);
btn->setTitleForState(CAControlStateAll, "Button");
btn->setTitleColorForState(CAControlStateNormal, CAColor_white);
btn->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage(CAImage::create("source_material/btn_rounded3D_highlighted.png")));
btn->setBackGroundViewForState(CAControlStateHighlighted, CAScale9ImageView::createWithImage(CAImage::create("source_material/btn_rounded3D_selected.png")));
btn->addTarget(this, CAControl_selector(MainMenuViewController::buttonCallback), CAControlEventTouchUpInSide);
this->getView()->addSubview(btn);
}
void MainMenuViewController::buttonCallback(CAControl* btn,CCPoint point)
{
auto button=dynamic_cast<CAButton*>(btn);
//TODO
}<span style="font-size:18px;">
</span>