cocos2d-x高階UI控制元件詳細介紹
對應官網的CCControlExtention。可以通過TestCpp的EntentionTest檢視效果。
http://cocos2d-x.org/projects/cocos2d-x/wiki/CCControlExtension
以下內容使用了擴充套件庫,因此要新增庫路徑和名稱空間
#include "cocos-ext.h"
USING_NS_CC_EXT;
並在屬性中配置extension路徑,加入libextensions.lib庫
CCScale9Sprite——九宮格Sprite
按照九宮格的方式縮放圖片的工具,和android的9patch一樣作用。可以使得按鈕變得漂亮起來。但不能處理點選事件。
例子:
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCRect rect = CCRectMake(0,0,15,32); //從原始圖片中讀取的範圍,引數分別為座標x,y和寬度,高度
CCRect rect_inside = CCRectMake(4,4,7,24); //原始圖片中,可變的部分,即用來放大的。引數解釋同上(x,y,width,height)。
CCSize rect_9Size = CCSizeMake(screenSize.width/2,screenSize.height/2);
CCScale9Sprite* button = cocos2d::extension::CCScale9Sprite::create("button.png", rect,rect_inside);
//button->setContentSize(CCSizeMake(screenSize.width, 57));
button->setPreferredSize(rect_9Size); //希望得到的最終按鈕大小
button->setPosition(ccp(screenSize.width/2, screenSize.height/ 2.0f));
addChild(button);
bool CCScale9Sprite::initWithFile(const char* file, CCRect rect, CCRect capInsets)
{
CCAssert(file != NULL, "Invalid file for sprite");
CCSpriteBatchNode *batchnode = CCSpriteBatchNode::create(file, 9);
bool pReturn = this->initWithBatchNode(batchnode, rect, capInsets);
return pReturn;
}
bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets)
{
if(batchnode)
{
this->updateWithBatchNode(batchnode, rect, rotated, capInsets);
this->setAnchorPoint(ccp(0.5f, 0.5f));
}
this->m_positionsAreDirty = true;
return true;
}
這就是button.png,可以在cocos2d-x的專案中搜到,是一個漸變圖。放大後效果
CCControlButton——事件按鈕
/** Number of kinds of control event. */
#define kControlEventTotalNumber 9
/** Kinds of possible events for the control objects. */
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.
};
typedef unsigned int CCControlEvent;
/** The possible state for a control. */
enum
{
CCControlStateNormal = 1 << 0, // The normal, or default state of a control—that is, enabled but neither selected nor highlighted.
CCControlStateHighlighted = 1 << 1, // Highlighted state of a control. A control enters this state when a touch down, drag inside or drag enter is performed. You can retrieve and set this value through the highlighted property.
CCControlStateDisabled = 1 << 2, // Disabled state of a control. This state indicates that the control is currently disabled. You can retrieve and set this value through the enabled property.
CCControlStateSelected = 1 << 3 // Selected state of a control. This state indicates that the control is currently selected. You can retrieve and set this value through the selected property.
};
typedef unsigned int CCControlState;
/** Number of kinds of control event. */
#define kControlEventTotalNumber 9
使用例子:(記得在標頭檔案宣告時引入標頭檔案、命名控制元件,配置CCControl.h的路徑,因為用到了CCControlEvent進行宣告)
cocos2d::extension::CCScale9Sprite *backgroundButton = cocos2d::extension::CCScale9Sprite::create("button.png");
cocos2d::extension::CCScale9Sprite *backgroundHighlightedButton = cocos2d::extension::CCScale9Sprite::create("buttonHighlighted.png");
// Creates a button with this string as title
CCLabelTTF *titleButton = CCLabelTTF::create("hello", "Marker Felt", 30);
titleButton->setColor(ccc3(159, 168, 176));
cocos2d::extension::CCControlButton *newbutton = cocos2d::extension::CCControlButton::create(titleButton, backgroundButton);
newbutton->setBackgroundSpriteForState(backgroundHighlightedButton, cocos2d::extension::CCControlStateHighlighted);
newbutton->setTitleColorForState(ccWHITE, cocos2d::extension::CCControlStateHighlighted);
newbutton->setPosition(ccp (screenSize.width / 2, screenSize.height / 2));
newbutton->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchDown),CCControlEventTouchDown);
//注意,addTargetWithActionForControlEvents是public的,addTargetWithActionForControlEvent是protected的,不要少了s,否則會報錯:無法訪問 protected 成員
// addTargetWithActionForControlEvents迴圈呼叫了addTargetWithActionForControlEvent
addChild(newbutton);
可參考以下程式碼
void CCControl::addTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents)
{
// For each control events
for (int i = 0; i < kControlEventTotalNumber; i++)
{
// If the given controlEvents bitmask contains the curent event
if ((controlEvents & (1 << i)))
{
this->addTargetWithActionForControlEvent(target, action, 1<<i);
}
}
}
CCControlSlider——拖動條
CCControlColorPicker——顏色選擇器
CCControlSwitch——開關控制元件
CCControlPotentionmeter——壓力計
CCControlStepper——分段控制元件
CCScrollView——滾動檢視
CCTabelView——列表檢視
CCTableViewCell* TableViewTestLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
CCString *string = CCString::createWithFormat("%d", idx);
CCTableViewCell *cell = table->dequeueCell();
if (!cell) {
cell = new CustomTableViewCell();
cell->autorelease();
CCSprite *sprite = CCSprite::create("Images/Icon.png");
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(ccp(0, 0));
cell->addChild(sprite);
CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Helvetica", 20.0);
label->setPosition(CCPointZero);
label->setAnchorPoint(CCPointZero);
label->setTag(123); //設定tag
cell->addChild(label);
}
else
{
CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(123); //根據tag獲取已經初始化的cell
label->setString(string->getCString());
}
return cell;
}
要使用tableVIew要記得繼承介面
class TableViewTestLayer : public cocos2d::CCLayer, public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate
CCTableViewDataSource的初始化函式
static CCTableView* create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container);
從資料來源 public cocos2d::extension::CCTableViewDataSource裡面繼承了以下函式。
virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
而CCTableViewDelegate 和又是繼承ScrollView的
class CCTableViewDelegate : public CCScrollViewDelegate。要實現以下兩個介面(如果沒有事件處理,可以不寫方法到{}裡)
virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view) {};
virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view) {}
CCTableViewDelegate 可以呼叫以下函式,反饋點選的Cell
void TableViewTestLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
CCLOG("cell touched at index: %i", cell->getIdx());
}
點選事件結束時候,判斷哪個cell被點選(可以借鑑這種方法去定義其它控制元件)
void CCTableView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
if (!this->isVisible()) {
return;
}
if (m_pTouches->count() == 1 && !this->isTouchMoved()) {
unsigned int index;
CCTableViewCell *cell;
CCPoint point;
point = this->getContainer()->convertTouchToNodeSpace(pTouch);
if (m_eVordering == kCCTableViewFillTopDown) {
CCSize cellSize = m_pDataSource->cellSizeForTable(this);
point.y -= cellSize.height;
}
index = this->_indexFromOffset(point);
cell = this->_cellWithIndex(index);
if (cell) {
m_pTableViewDelegate->tableCellTouched(this, cell); //注意在這裡呼叫tableCellTouched
}
}
CCScrollView::ccTouchEnded(pTouch, pEvent);
}
相關文章
- javascript this詳細介紹JavaScript
- JDBC 詳細介紹JDBC
- Kafka詳細介紹Kafka
- Git詳細介紹Git
- React 高階元件介紹React元件
- Chart FX for Java Server控制元件下載及詳細介紹JavaServer控制元件
- Go Channel 詳細介紹Go
- Nacos 介面詳細介紹
- MQ詳細命令介紹MQ
- Recovery命令詳細介紹
- Vmstat 命令詳細介紹
- SAP UI5 DynamicPage 控制元件介紹UI控制元件
- android 自定義控制元件 自定義屬性詳細介紹Android控制元件
- MongoDB高階查詢詳細MongoDB
- Flutter系列(一)——詳細介紹Flutter
- Nginx服務詳細介紹Nginx
- python字典詳細介紹Python
- Spring bean詳細介紹SpringBean
- Http Module 的詳細介紹HTTP
- Java異常詳細介紹Java
- 【SCN】Oracle SCN 詳細介紹Oracle
- SOLIDWORKS API詳細介紹SolidAPI
- Webpack 打包 Javascript 詳細介紹WebJavaScript
- mysql binlog詳細介紹MySql
- java泛型詳細介紹Java泛型
- 【工具】Sublime使用詳細介紹
- asmcmd工具的詳細介紹ASM
- json詳細介紹(for Java)JSONJava
- Mysqldump工具的詳細介紹MySql
- rman超詳細命令介紹
- Aix 上NFS詳細介紹AINFS
- ApplicationContext 詳細介紹APPContext
- SAP UI5 FlexibleColumnLayout 控制元件介紹UIFlex控制元件
- winform中更新UI控制元件的方案介紹ORMUI控制元件
- javascript高階函式的介紹JavaScript函式
- 「Oracle」Oracle高階查詢介紹Oracle
- TreeViewTemplate移動元件詳細介紹View元件
- pyhanlp 文字聚類詳細介紹HanLP聚類