Cocos2d-x CCScrollView demo

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");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值