Cocos2d-x CCScrollView demo

来源:互联网 发布:零售门店经营数据分析 编辑:程序博客网 时间:2024/04/30 14:25

TestScrollView.h

////  TestScrollView.h//  Demo////  Created by kodeyang on 8/22/13.////#ifndef __Demo__TestScrollView__#define __Demo__TestScrollView__#include "cocos2d.h"#include "cocos-ext.h"#include "DCMacros.h"USING_NS_CC;USING_NS_CC_EXT;class TestScrollView : public CCLayer, CCScrollViewDelegate {public:        SCENE_FUNC(TestScrollView);        CREATE_FUNC(TestScrollView);        virtual bool init();        virtual void scrollViewDidScroll(CCScrollView* view);        virtual void scrollViewDidZoom(CCScrollView* view);    private:        CCLayer* m_pLyrContainer;        CCScrollView* m_pLyrScroll;};#endif /* defined(__Demo__TestScrollView__) */

TestScrollView.cpp

////  TestScrollView.cpp//  Demo////  Created by kodeyang on 8/22/13.////#include "TestScrollView.h"bool TestScrollView::init() {        if (!CCLayer::init()) {        return false;    }        CCDirector* t_pDirector = CCDirector::sharedDirector();    CCSize t_oSize = t_pDirector->getWinSize();        // 容器层的尺寸    int t_iContainerWidth = t_oSize.width;    int t_iContainerHeight = t_oSize.height;        // 卷展层的实际显示尺寸    int t_iScrollWidth = t_oSize.width;    int t_iScrollHeight = t_oSize.height * 0.5f;        // 容器层初始化    ccColor4B t_oC4bTop = ccc4(255, 0, 0, 255);    ccColor4B t_oC4bBottom = ccc4(0, 255, 0, 128);    m_pLyrContainer = CCLayerGradient::create(t_oC4bTop, t_oC4bBottom);    m_pLyrContainer->setContentSize(CCSizeMake(t_iContainerWidth, t_iContainerHeight));    m_pLyrContainer->setPosition(ccp(0, t_iScrollHeight - t_iContainerHeight));        // 为容器层添加几个物件    CCSprite* t_pSp0 = CCSprite::create("btn.png");    t_pSp0->setPosition(ccp(t_oSize.width * 0.5f, t_oSize.height * 0.6f));    m_pLyrContainer->addChild(t_pSp0);        CCSprite* t_pSp1 = CCSprite::create("btn_hover.png");    t_pSp1->setPosition(ccp(t_oSize.width * 0.5f, t_oSize.height * 0.4f));    m_pLyrContainer->addChild(t_pSp1);        // 卷展层初始化    m_pLyrScroll = CCScrollView::create();    m_pLyrScroll->setViewSize(CCSizeMake(t_iScrollWidth, t_iScrollHeight));    m_pLyrScroll->ignoreAnchorPointForPosition(false);    m_pLyrScroll->setPosition(ccp(t_oSize.width * 0.5f, t_oSize.height * 0.5f));    m_pLyrScroll->setContentSize(m_pLyrContainer->getContentSize());    m_pLyrScroll->setDirection(kCCScrollViewDirectionVertical);    m_pLyrScroll->setContainer(m_pLyrContainer);    m_pLyrScroll->setBounceable(true);    m_pLyrScroll->setDelegate(this);        this->addChild(m_pLyrScroll);        return true;}void TestScrollView::scrollViewDidScroll(CCScrollView* view) {    float t_fOffsetY = m_pLyrScroll->getContentOffset().y;    printf("scroll, contentOffsetY = %f\n", t_fOffsetY);}void TestScrollView::scrollViewDidZoom(CCScrollView* view) {    printf("zoom\n");}

原创粉丝点击