cocos2d-x中的CCScrollView的使用
寫了一個關於CCScrollView的小例子,跟大家分享一下,下面是代碼
ChatScrollView.h文件
//
// ChatScrollView.h
// FontLabel
//
// Created by 江南岸 on 13-3-13.
//
//
#ifndef __FontLabel__ChatScrollView__
#define __FontLabel__ChatScrollView__
#include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class ChatScrollView : public CCLayer
{
private:
int scrollHeight;
CCScrollView *scrollview;
void viewDidLoad();
void buttonClick();
public:
ChatScrollView();
~ChatScrollView();
static ChatScrollView *create();
};
#endif /* defined(__FontLabel__ChatScrollView__) */
下面是ChatScrollView.cpp文件
//
// ChatScrollView.cpp
// FontLabel
//
// Created by 江南岸 on 13-3-13.
//
//
#include "ChatScrollView.h"
#include "ChatScrollViewSubview.h"
ChatScrollView::ChatScrollView()
{
scrollHeight = 0;
}
ChatScrollView::~ChatScrollView()
{
}
ChatScrollView* ChatScrollView::create()
{
ChatScrollView *chat = new ChatScrollView();
if(chat)
{
chat->viewDidLoad();
chat->autorelease();
return chat;
}
CC_SAFE_DELETE(chat);
return NULL;
}
/*
* CCScrollView裏邊有個Container用來保存它的subview
* 它的subview我們採用CCLayerColor 因爲它可以設置大小
*/
//初始化函數
void ChatScrollView::viewDidLoad()
{
int name = 0;
int chat = 0;
//設置ScrollView的顯示大小
scrollview = CCScrollView::create(CCSizeMake(400, 320));
//初始化ScrollView的container
CCLayer *layer = CCLayer::create();
//高度計數器,設置每個CCLayerColor的偏移量,循環完後這就是整個ScrollView的ContentSize
int heigth = 0;
for(int i=0;i<20;i++)
{
ChatScrollViewSubview *subview = ChatScrollViewSubview::create(CCString::createWithFormat("%d",name),CCString::createWithFormat("%d",chat),CCSizeMake(400, 100));
//注意,這裏是作爲container的CCLayer添加
layer->addChild(subview);
//設置偏移量
subview->setPosition(ccp(0, heigth));
//偏移量計數器加加
heigth += 110;
name++;
chat++;
}
//把layer設置爲container
scrollview->setContainer(layer);
//設置container的contentSize
layer->setContentSize(CCSizeMake(400, heigth));
//這裏設置的是可滑動區域的大小,並不是scrollview的大小
scrollview->setContentSize(CCSizeMake(400, heigth));
//設置方向,爲縱向
scrollview->setDirection(kCCScrollViewDirectionVertical);
//設置scrollView的位置
CCSize size = CCDirector::sharedDirector()->getWinSize();
scrollview->setPosition(ccp(size.width/2,100));
this->addChild(scrollview);
CCLabelTTF *labelStr = CCLabelTTF::create("BUTTON", "Arial", 24);
CCMenuItemLabel *itemLabel = CCMenuItemLabel::create(labelStr, this, menu_selector(ChatScrollView::buttonClick));
CCMenu *menu = CCMenu::create();
menu->addChild(itemLabel);
addChild(menu);
}
void ChatScrollView::buttonClick()
{
//點擊移動scrollView,設置偏移量
scrollHeight += 110;
scrollview->setContentOffsetInDuration(ccp(0, scrollHeight), 1.0f);
}
上面的類是設置CCScrollView的,下面我們來寫它的Subview
//
// ChatScrollViewSubview.h
// FontLabel
//
// Created by 江南岸 on 13-3-13.
//
//
#ifndef __FontLabel__ChatScrollViewSubview__
#define __FontLabel__ChatScrollViewSubview__
#include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
class ChatScrollViewSubview : public CCLayerColor
{
private:
CCString *_chatStr;
CCString *_nameStr;
CCSize _size;
void viewDiaLoad();
public:
ChatScrollViewSubview();
~ChatScrollViewSubview();
static ChatScrollViewSubview *create(CCString *chatStr,CCString *nameStr,CCSize size);
};
#endif /* defined(__FontLabel__ChatScrollViewSubview__) */
下面是.cpp文件
//
// ChatScrollViewSubview.cpp
// FontLabel
//
// Created by 江南岸 on 13-3-13.
//
//
#include "ChatScrollViewSubview.h"
ChatScrollViewSubview::ChatScrollViewSubview()
{
}
ChatScrollViewSubview::~ChatScrollViewSubview()
{
_chatStr->release();
_nameStr->release();
}
ChatScrollViewSubview* ChatScrollViewSubview::create(CCString *chatStr,CCString *nameStr,CCSize size)
{
ChatScrollViewSubview *subview = new ChatScrollViewSubview();
subview->_chatStr = chatStr;
subview->_chatStr->retain();
subview->_nameStr = nameStr;
subview->_nameStr->retain();
subview->_size = size;
if(subview && subview->initWithColor(ccc4(255, 255, 100, 255), size.width, size.height))
{
subview->viewDiaLoad();
subview->autorelease();
return subview;
}
CC_SAFE_DELETE(subview);
return subview;
}
void ChatScrollViewSubview::viewDiaLoad()
{
CCLabelTTF *labelName = CCLabelTTF::create(_nameStr->getCString(), "Arial", 24);
CCLabelTTF *labelChat = CCLabelTTF::create(_chatStr->getCString(), "Arial", 24);
labelName->setPosition(ccp(50, 50));
labelChat->setPosition(ccp(100, 50));
labelChat->setColor(ccBLACK);
labelName->setColor(ccBLACK);
addChild(labelName);
addChild(labelChat);
}
這只是對CCScrollView的一點簡單的使用,希望對大家能夠有幫助。。。。。。。