利用数组元素交换技术来实现界面的翻页滑动效果

来源:互联网 发布:freebsd和mac 编辑:程序博客网 时间:2024/05/29 10:39
#ifndef _GrowLayer_#define _GrowLayer_#include "BasicLayer.h"#include "cocos-ext.h"USING_NS_CC_EXT;/*1,成长选项卡2,里面内置滚动层*/class GrowLayer : public BasicLayer{public:virtual bool init();CREATE_FUNC(GrowLayer);void setView();~GrowLayer();public:CCPoint point;CCArray* ary;  //存放5个精灵CCSize size0;  //滚动层每个界面对应的大小CCSize size1;CCSize size2;CCSize size3;CCSize size4;int select;    //当前界面索引void updateViewForword();void updateViewBack();public:virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);virtual void onEnter();virtual void onExit();CCPoint touchBeginPoint;  //开始触摸点CCPoint touchEndPoint;    //结束触摸点};#endif/**************************************实现*******************************/#include "GrowLayer.h"#include "ScrollLock.h"GrowLayer::~GrowLayer(){CC_SAFE_DELETE(ary);}bool GrowLayer::init(){if(!BasicLayer::init()) return false;ary = CCArray::create();ary->retain();setView();return true;}void GrowLayer::setView(){point = ccp(winWidth*0.7, winHeight*0.48);//这个是变化的部分CCSprite* sprite1=CCSprite::createWithSpriteFrameName("4.png");size0 = sprite1->getContentSize();sprite1->setTag(0);addChild(sprite1);ary->addObject(sprite1);CCSprite* sprite2=CCSprite::createWithSpriteFrameName("1.png");size1 = sprite2->getContentSize();sprite2->setTag(1);addChild(sprite2);ary->addObject(sprite2);CCSprite* smallbackground = CCSprite::createWithSpriteFrameName("smallbackground.png");size2 = smallbackground->getContentSize();smallbackground->setTag(2);addChild(smallbackground);ary->addObject(smallbackground);CCSprite* sprite3=CCSprite::createWithSpriteFrameName("2.png");size3 = sprite3->getContentSize();sprite3->setTag(3);addChild(sprite3);ary->addObject(sprite3);CCSprite* sprite4=CCSprite::createWithSpriteFrameName("3.png");size4 = sprite4->getContentSize();sprite4->setTag(4);addChild(sprite4);ary->addObject(sprite4);updateViewBack();  //这样才能实现界面的刷新updateViewForword();}void GrowLayer::updateViewBack(){for(int i = 0; i < 4; i++){ary->exchangeObjectAtIndex(i,i+1);}//移除原来的节点for(int i = 0; i < 5; i++){removeChildByTag(i);}//设置一下Z轴,再次添加节点for(int i = 0; i < 5; i++){switch(i){case 0:{((CCSprite*)(ary->objectAtIndex(0)))->setZOrder(1);float _width = ((CCSprite*)(ary->objectAtIndex(0)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(0)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(0)))->setScaleX(size0.width / _width);((CCSprite*)(ary->objectAtIndex(0)))->setScaleY(size0.width / _height);((CCSprite*)(ary->objectAtIndex(0)))->setPosition(ccp(point.x - 130, point.y));addChild((CCSprite*)(ary->objectAtIndex(0)));}break;case 1:{    ((CCSprite*)(ary->objectAtIndex(1)))->setZOrder(2);float _width = ((CCSprite*)(ary->objectAtIndex(1)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(1)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(1)))->setScaleX(size1.width / _width);((CCSprite*)(ary->objectAtIndex(1)))->setScaleY(size1.width / _height);((CCSprite*)(ary->objectAtIndex(1)))->setPosition(ccp(point.x - 80, point.y));    addChild((CCSprite*)(ary->objectAtIndex(1)));}    break;case 2:{((CCSprite*)(ary->objectAtIndex(2)))->setZOrder(3);float _width = ((CCSprite*)(ary->objectAtIndex(2)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(2)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(2)))->setScaleX((size3.width+50) / _width);((CCSprite*)(ary->objectAtIndex(2)))->setScaleY((size3.width+50) / _height);((CCSprite*)(ary->objectAtIndex(2)))->setPosition(point);addChild((CCSprite*)(ary->objectAtIndex(2)));}break;case 3:{((CCSprite*)(ary->objectAtIndex(3)))->setZOrder(2);float _width = ((CCSprite*)(ary->objectAtIndex(3)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(3)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(3)))->setScaleX(size3.width / _width);((CCSprite*)(ary->objectAtIndex(3)))->setScaleY(size3.width / _height);((CCSprite*)(ary->objectAtIndex(3)))->setPosition(ccp(point.x + 80, point.y));addChild((CCSprite*)(ary->objectAtIndex(3)));}break;case 4:{((CCSprite*)(ary->objectAtIndex(4)))->setZOrder(1);float _width = ((CCSprite*)(ary->objectAtIndex(4)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(4)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(4)))->setScaleX(size4.width / _width);((CCSprite*)(ary->objectAtIndex(4)))->setScaleY(size4.width / _height);((CCSprite*)(ary->objectAtIndex(4)))->setPosition(ccp(point.x + 130, point.y));addChild((CCSprite*)(ary->objectAtIndex(4)));}break;}}}void GrowLayer::updateViewForword(){for(int i = 3; i >= 0; i--){ary->exchangeObjectAtIndex(i,i+1);}//移除原来的节点for(int i = 0; i < 5; i++){removeChildByTag(i);}//设置一下Z轴,再次添加节点for(int i = 0; i < 5; i++){switch(i){case 0:{((CCSprite*)(ary->objectAtIndex(0)))->setZOrder(1);float _width = ((CCSprite*)(ary->objectAtIndex(0)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(0)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(0)))->setScaleX(size0.width / _width);((CCSprite*)(ary->objectAtIndex(0)))->setScaleY(size0.width / _height);((CCSprite*)(ary->objectAtIndex(0)))->setPosition(ccp(point.x - 130, point.y));addChild((CCSprite*)(ary->objectAtIndex(0)));}break;case 1:{((CCSprite*)(ary->objectAtIndex(1)))->setZOrder(2);float _width = ((CCSprite*)(ary->objectAtIndex(1)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(1)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(1)))->setScaleX(size1.width / _width);((CCSprite*)(ary->objectAtIndex(1)))->setScaleY(size1.width / _height);((CCSprite*)(ary->objectAtIndex(1)))->setPosition(ccp(point.x - 80, point.y));addChild((CCSprite*)(ary->objectAtIndex(1)));}break;case 2:    //显然,这个是当前选中选项{((CCSprite*)(ary->objectAtIndex(2)))->setZOrder(3);float _width = ((CCSprite*)(ary->objectAtIndex(2)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(2)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(2)))->setScaleX((size3.width+50) / _width);((CCSprite*)(ary->objectAtIndex(2)))->setScaleY((size3.width+50) / _height);((CCSprite*)(ary->objectAtIndex(2)))->setPosition(point);addChild((CCSprite*)(ary->objectAtIndex(2)));}break;case 3:{((CCSprite*)(ary->objectAtIndex(3)))->setZOrder(2);float _width = ((CCSprite*)(ary->objectAtIndex(3)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(3)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(3)))->setScaleX(size3.width / _width);((CCSprite*)(ary->objectAtIndex(3)))->setScaleY(size3.width / _height);((CCSprite*)(ary->objectAtIndex(3)))->setPosition(ccp(point.x + 80, point.y));addChild((CCSprite*)(ary->objectAtIndex(3)));}break;case 4:{((CCSprite*)(ary->objectAtIndex(4)))->setZOrder(1);float _width = ((CCSprite*)(ary->objectAtIndex(4)))->getContentSize().width;float _height = ((CCSprite*)(ary->objectAtIndex(4)))->getContentSize().height;((CCSprite*)(ary->objectAtIndex(4)))->setScaleX(size4.width / _width);((CCSprite*)(ary->objectAtIndex(4)))->setScaleY(size4.width / _height);((CCSprite*)(ary->objectAtIndex(4)))->setPosition(ccp(point.x + 130, point.y));addChild((CCSprite*)(ary->objectAtIndex(4)));}break;}}}bool GrowLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){touchBeginPoint = convertToWorldSpace(pTouch->getLocationInView());return true;}void GrowLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){}void GrowLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){touchEndPoint = convertToWorldSpace(pTouch->getLocationInView());if(touchBeginPoint.x > touchEndPoint.x){updateViewBack();}else{updateViewForword();}}void GrowLayer::onEnter(){BasicLayer::onEnter();CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -125, true);}void GrowLayer::onExit(){CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);BasicLayer::onExit();}

0 0
原创粉丝点击