cocos2d-x中的CCScrollView的使用

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的一点简单的使用,希望对大家能够有帮助。。。。。。。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章