Cocos2d-x_CCSrollView(滚动视图类)介绍

前端之家收集整理的这篇文章主要介绍了Cocos2d-x_CCSrollView(滚动视图类)介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer,public extension::CCScrollViewDelegate  //CCScrollViewDelegate
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);
    
    // 必须重写和定义CCScrollViewDelegate的协议,要不然CREATE_FUNC宏会报错
    virtual void scrollViewDidScroll(CCScrollView* view);
    virtual void scrollViewDidZoom(CCScrollView* view);
};

#endif

//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    // 创建一个layer作为容器
    CCLayer *containLayer = CCLayer::create();
    
    CCSprite *pSpr1 = CCSprite::create("HelloWorld.png");
    pSpr1->setPosition(ccp(winSize.width*0.5,winSize.height*0.5));
    
    CCSprite *pSpr2 = CCSprite::create("HelloWorld.png");
    pSpr2->setPosition(ccp(pSpr1->getContentSize().width,winSize.height*0.5));
    
    containLayer->addChild(pSpr1);
    containLayer->addChild(pSpr2);
    containLayer->setContentSize(CCSizeMake(winSize.width*3,winSize.height));
    
    // 创建CCScrollView
    CCScrollView *pScrView = CCScrollView::create(CCSizeMake(winSize.width*2,winSize.height));
    pScrView->setBounceable(false);
    pScrView->setContainer(containLayer);
    
    // 必须在AppController.mm中开启ios对多触点的支持
    //[__glView setMultipleTouchEnabled:YES];
    pScrView->setTouchEnabled(true);
    pScrView->setDelegate(this);
    this->addChild(pScrView);
    
    return true;
}

void HelloWorld::scrollViewDidScroll(cocos2d::extension::CCScrollView *view)
{
    CCLog("scrView移动");
}

void HelloWorld::scrollViewDidZoom(cocos2d::extension::CCScrollView *view)
{
    CCLog("scrView缩放");
}
</pre><pre name="code" class="cpp">

猜你在找的Cocos2d-x相关文章