在需要用的时候,包括就可以了。
我们在相应的申明的地方,包含下面三个头文件
#include "GUI/CCControlExtension/CCControlButton.h"
#include "GUI/CCControlExtension/CCScale9Sprite.h"
#include "GUI/CCControlExtension/CCControl.h"
enum
{
CCControlEventTouchDown = 1 << 0, // A touch-down event in the control.
CCControlEventTouchDragInside = 1 << 1, // An event where a finger is dragged inside the bounds of the control.
CCControlEventTouchDragOutside = 1 << 2, // An event where a finger is dragged just outside the bounds of the control.
CCControlEventTouchDragEnter = 1 << 3, // An event where a finger is dragged into the bounds of the control.
CCControlEventTouchDragExit = 1 << 4, // An event where a finger is dragged from within a control to outside its bounds.
CCControlEventTouchUpInside = 1 << 5, // A touch-up event in the control where the finger is inside the bounds of the control.
CCControlEventTouchUpOutside = 1 << 6, // A touch-up event in the control where the finger is outside the bounds of the control.
CCControlEventTouchCancel = 1 << 7, // A system event canceling the current touches for the control.
CCControlEventValueChanged = 1 << 8 // A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
};
在这里,定义了事件函数 void OnTouchDown(CCObject * pSender, extension::CCControlEvent event );
void OnTouchDragEnter(CCObject* pSender, extension::CCControlEvent event );
void OnTouchDragExit(CCObject* pSender, extension::CCControlEvent event );
void OnTouchDragInside(CCObject* pSender, extension::CCControlEvent event );
void OnTouchDragOutside(CCObject* pSender, extension::CCControlEvent event );
void OnTouchUpInside(CCObject* pSender, extension::CCControlEvent event );
void OnTouchUpOutside(CCObject* pSender, extension::CCControlEvent event );
void OnTouchCancel(CCObject* pSender, extension::CCControlEvent event );
void HelloWorld::OnTouchDown(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchDown:%u", event );
}
void HelloWorld::OnTouchDragEnter(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchDragEnter:%u", event );
}
void HelloWorld::OnTouchDragExit(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchDragExit:%u", event );
}
void HelloWorld::OnTouchDragInside(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchDragInside:%u", event );
}
void HelloWorld::OnTouchDragOutside(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchDragOutside:%u", event );
}
void HelloWorld::OnTouchUpInside(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchUpInside:%u", event );
}
void HelloWorld::OnTouchUpOutside(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchUpOutside:%u", event );
}
void HelloWorld::OnTouchCancel(CCObject* pSender, extension::CCControlEvent event )
{
PrintLog("OnTouchCancel:%u", event );
}
{
extension::CCScale9Sprite * pBtnNormal = extension::CCScale9Sprite::create("up.png");
extension::CCScale9Sprite * pBtnDown = extension::CCScale9Sprite::create("up.png");
CCLabelTTF * pBtnCaption = CCLabelTTF::create("", "宋体", 30);
extension::CCControlButton * pBtn = extension::CCControlButton::create(pBtnCaption, pBtnNormal);
pBtn->setBackgroundSpriteForState(pBtnDown, extension::CCControlStateSelected);
pBtn->setPosition(GetPosition(pPlayerLayer, "UpPoint"));
pBtn->setAnchorPoint(CCPointZero);
pBtn->setPreferredSize(CCSizeMake(32, 32));
pBtn->setTag(100);
this ->addChild(pBtn);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchDown), CCControlEventTouchDown);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchDragEnter), CCControlEventTouchDragEnter);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchDragExit), CCControlEventTouchDragExit);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchDragInside), CCControlEventTouchDragInside);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchDragOutside), CCControlEventTouchDragOutside);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchUpInside), CCControlEventTouchUpInside);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchUpOutside), CCControlEventTouchUpOutside);
pBtn->addTargetWithActionForControlEvents( this , cccontrol_selector(HelloWorld::OnTouchCancel), CCControlEventTouchCancel);
}
具体的按钮事件实验结果说明:
CCControlEventTouchDown:鼠标该处于按下并曾经点中按钮时,则触发一次
CCControlEventTouchDragEnter:当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次 ,也就是处于按下状态,回到该按钮区域
CCControlEventTouchDragExit:当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次 ,也就是处于按下状态,离开该按钮区域
CCControlEventTouchDragInside:当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,每产生移动,就会触发 ,
CCControlEventTouchDragOutside: 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,每产生移动,就会触发
CCControlEventTouchUpInside:当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次,也就是处于按下状态,并在该按钮区域放起
CCControlEventTouchUpOutside:当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次 也就是处于按下状态,但是已经离开该按钮区域,再放起
CCControlEventTouchCancel:暂时没有发现能用鼠标触发这个事件的操作
了解后,就可以根据实际要求实现了