cocos2d-js 控件——UIButton

UIButton公有属性、方法

/**
 * 创建一个UIButton实例
 * @param normalImage   默认状态时的图片
 * @param selectedImage 点击状态时的图片
 * @param disableImage  禁用状态时的图片
 * @param texType       图片类型(Local、Plist)
 * @return UIButton实例
 */
static Button* create(const std::string& normalImage,
                      const std::string& selectedImage = "",
                      const std::string& disableImage = "",
                      TextureResType texType = TextureResType::LOCAL);

/**
 * 加载UIbutton图片
 * @param normal    默认状态时的图片
 * @param selected  点击状态时的图片
 * @param disabled  禁用状态时的图片
 * @param texType   图片类型(Local、Plist)
 */
void loadTextures(const std::string& normal,
                  const std::string& selected,
                  const std::string& disabled = "",
                  TextureResType texType = TextureResType::LOCAL);

/**
 * 加载UIbutton默认状态下的图片
 * @param normal    图片
 * @param texType   图片类型(Local、Plist)
 */
void loadTextureNormal(const std::string& normal, TextureResType texType = TextureResType::LOCAL);

/**
 * 加载UIbutton点击状态下的图片
 * @param selected   图片
 * @param texType    图片类型(Local、Plist)
 */
void loadTexturePressed(const std::string& selected, TextureResType texType = TextureResType::LOCAL);

/**
 * 加载UIbutton禁用状态下的图片
 * @param disabled   图片
 * @param texType    图片类型(Local、Plist)
 */
void loadTextureDisabled(const std::string& disabled, TextureResType texType = TextureResType::LOCAL);

/**
 * 设置九宫格拉伸范围
 * 只有在调用setScale9Enabled(true)时,capInset才会影响所有按钮的scale9渲染器
 * @param capInsets 拉伸范围
 */
void setCapInsets(const Rect &capInsets);

/**
 * 设置默认状态下的capInsets 
 * @param capInsets 拉伸范围
 */
void setCapInsetsNormalRenderer(const Rect &capInsets);

/**
 * 获取默认状态下的capInsets
 * @return 拉伸范围
 */
const Rect& getCapInsetsNormalRenderer()const;

/**
 * 设置点击状态下的capInsets 
 * @param capInsets 拉伸范围
 */
void setCapInsetsPressedRenderer(const Rect &capInsets);

/**
 * 获取点击状态下的capInsets
 * @return 拉伸范围
 */
const Rect& getCapInsetsPressedRenderer()const;

/**
 * 设置禁用状态下的capInsets 
 * @param capInsets 拉伸范围
 */
void setCapInsetsDisabledRenderer(const Rect &capInsets);

/**
 * 获取禁用状态下的capInsets
 * @return 拉伸范围
 */
const Rect& getCapInsetsDisabledRenderer()const;

/**
 * 启用scale9渲染
 * @param 是否启用
 */
virtual void setScale9Enabled(bool enable);

/**
 * 查询是否启用scale9渲染
 * @return true 或 false
 */
bool isScale9Enabled()const;

/**
 * 设置UIButton在按下时是否进行缩放
 * @param true 或 false
 */
void setPressedActionEnabled(bool enabled);

/**
 * 设置UIButton的标题
 * @param text 标题内容
 */
void setTitleText(const std::string& text);

/**
 * 获取UIbutton的标题
 * @return 标题内容
 */
const std::string getTitleText() const;

/**
 * 设置UIButton的标题颜色
 * @param color 标题颜色
 */
void setTitleColor(const Color3B& color);

/**
 * 获取UIButton的标题颜色
 * @return 标题颜色
 */
Color3B getTitleColor() const;

/**
 * 设置UIButton的标题大小
 * @param size 标题大小
 */
void setTitleFontSize(float size);

/**
 * 获取UIButton的标题大小
 * @return 标题大小
 */
float getTitleFontSize() const;

/**
 * 设置UIButton的标题字体
 * @param fontName 字体
 */
void setTitleFontName(const std::string& fontName);

/**
 * 获取UIButton的标题字体
 * @return 字体
 */
const std::string getTitleFontName() const;

/**
 * 设置UIButton的标题水平对齐
 * @param hAlignment 水平对齐
 */
void setTitleAlignment(TextHAlignment hAlignment);

/**
 * 设置UIButton的标题水平对齐、垂直对齐
 * @param hAlignment 水平对齐
 * @param vAlignment 垂直对齐
 */
void setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment);

/** 
 * 当用户按下按钮时,按钮将缩放到一个比例。
 * 按钮的最终比例等于(按钮原始比例+ _zoomScale)
 * @param scale 缩放比例
 */
void setZoomScale(float scale);

/**
 * 返回一个按钮的缩放比例
 * @return 缩放比例
 */
float getZoomScale()const;

/**
 * 返回默认状态下的九宫格精灵
 * @return 默认状态下的九宫格精灵
 */
Scale9Sprite* getRendererNormal() const { return _buttonNormalRenderer; }

/**
 * 返回点击状态下的九宫格精灵
 * @return 点击状态下的九宫格精灵
 */
Scale9Sprite* getRendererClicked() const { return _buttonClickedRenderer; }

/**
 * 返回禁用状态下的九宫格精灵
 * @return 禁用状态下的九宫格精灵
 */
Scale9Sprite* getRendererDisabled() const { return _buttonDisabledRenderer; }

UIButton 示例

// 初始化,参数分别是默认状态、按下状态、禁用状态下的图片
var button = new ccui.Button("", "", "");  

// 锚点
button.setAnchorPoint(0.5, 0.5);

// 设置图片(正常情况下的图片、点击时的图片、禁用时的图片)
button.loadTextures("res/a.png", "res/b.png", "res/c.png");

// 位置
button.setPosition(10, 10);  

// 设置是否伴随点击缩放按钮图片  
emojiBtn.setPressedActionEnabled(false);

// 按钮标题
button.setTitleText(emojiList[i]);

// 按钮标题大小
button.setTitleFontSize(40);

// 按钮标题颜色 两者相同
button.setTitleColor(cc.color(53,53,53))
button.setColor(cc.color(0, 0, 0, 255));

// 设置字体样式
button.setTitleFontName("宋体,微软雅黑");

// 允许按钮穿透(解决在tableView上无法滚动的问题)
button.setSwallowTouches(false);

// 透明按钮(先设置图片,然后再调用loadTextures,参数传入"")
var copy_button = new ccui.Button(this.btn_back_img, this.btn_back_img, "", 1);
copy_button.loadTextures("", "", "");

// 设置按钮回调
button.addTouchEventListener( function(sender, type) {
    if (type == ccui.Widget.TOUCH_ENDED) {

    }
}.bind(this), this);

this.addChild(button);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值