直接上代码
MyScrollView.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 // 2 // MyScrollView.h 3 // ScrollViewTest 4 // 5 // Created by HanHongmin on 14-1-1. 6 // 7 // 8 9 #ifndef __ScrollViewTest__MyScrollView__10 #define __ScrollViewTest__MyScrollView__11 12 #include "cocos2d.h"13 #include "cocos-ext.h"14 using namespace cocos2d;15 using namespace cocos2d::extension;16 17 class MyScrollView:public CCScrollView{18 public:19 static MyScrollView* create();20 static MyScrollView* create(CCSize size);21 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);22 virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);23 virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);24 virtual void adjustScrollView();25 };26 27 #endif /* defined(__ScrollViewTest__MyScrollView__) */
MyScrollView.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 // 2 // MyScrollView.cpp 3 // ScrollViewTest 4 // 5 // Created by HanHongmin on 14-1-1. 6 // 7 // 8 9 #include "MyScrollView.h"10 11 12 MyScrollView* MyScrollView::create()13 {14 MyScrollView* pRet = new MyScrollView();15 if (pRet && pRet->init())16 {17 pRet->autorelease();18 }19 else20 {21 CC_SAFE_DELETE(pRet);22 }23 return pRet;24 }25 26 MyScrollView* MyScrollView::create(CCSize size)27 {28 MyScrollView* pRet = new MyScrollView();29 if (pRet && pRet->initWithViewSize(size, NULL))30 {31 pRet->autorelease();32 }33 else34 {35 CC_SAFE_DELETE(pRet);36 }37 return pRet;38 }39 40 41 42 bool MyScrollView::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){43 bool b = CCScrollView::ccTouchBegan(pTouch, pEvent);44 CCLog("MyScrollView::ccTouchBegan");45 return b;46 }47 void MyScrollView::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){48 CCScrollView::ccTouchMoved(pTouch, pEvent);49 CCLog("MyScrollView::ccTouchMoved");50 //阻止传递51 }52 void MyScrollView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){53 CCScrollView::ccTouchEnded(pTouch, pEvent);54 CCLog("MyScrollView::ccTouchEnded");55 adjustScrollView();56 //不在往下传递事件57 }58 59 void MyScrollView::adjustScrollView(){60 // 关闭CCScrollView中的自调整61 this->unscheduleAllSelectors();62 63 int x = this->getContentOffset().x;64 int offset = (int) x % 640;65 // 调整位置66 CCPoint adjustPos;67 // 调整动画时间68 float adjustAnimDelay = 0.15f;69 70 if (offset < -320) { //翻到下一页71 // 计算下一页位置,时间72 adjustPos = ccpSub(this->getContentOffset(), ccp(640 + offset, 0));73 }74 else { //留在当前页75 // 计算当前页位置,时间76 adjustPos = ccpSub(this->getContentOffset(), ccp(offset, 0));77 }78 if(adjustPos.x< -(this->getContentSize().width-640)){ //超过最后一页的限制79 adjustPos = CCPointMake(-(this->getContentSize().width-640), 0);80 }81 // 调整位置82 this->setContentOffsetInDuration(adjustPos, adjustAnimDelay);83 }
HelloWorldScene.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 #include "cocos-ext.h" 6 #include "VisibleRect.h" 7 #include "MyScrollView.h" 8 using namespace cocos2d; 9 using namespace cocos2d::extension;10 11 class HelloWorld : public CCLayer,public CCScrollViewDelegate12 {13 public:14 virtual bool init();15 static cocos2d::CCScene* scene();16 void buttonClick();17 18 virtual void scrollViewDidScroll(CCScrollView* view);19 virtual void scrollViewDidZoom(CCScrollView* view);20 21 CREATE_FUNC(HelloWorld);22 protected:23 MyScrollView *_scrollView;24 };25 26 #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include "HelloWorldScene.h" 2 3 USING_NS_CC; 4 5 CCScene* HelloWorld::scene() 6 { 7 CCScene *scene = CCScene::create(); 8 HelloWorld *layer = HelloWorld::create(); 9 scene->addChild(layer);10 return scene;11 }12 13 // on "init" you need to initialize your instance14 bool HelloWorld::init()15 {16 if ( !CCLayer::init() )17 {18 return false;19 }20 CCSprite *bg = CCSprite::create("Default-568h@2x.png");21 bg->setPosition(VisibleRect::center());22 this->addChild(bg);23 24 CCMenuItemImage *button = CCMenuItemImage::create("Icon-144.png", "Icon-144.png", this, menu_selector(HelloWorld::buttonClick));25 CCMenu *menu = CCMenu::create(button,NULL);26 menu->setPosition(CCPointZero);27 button->setPosition(ccp(VisibleRect::leftTop().x+72,VisibleRect::leftTop().y-72));28 this->addChild(menu);29 30 CCLayer *container = CCLayer::create();31 container->setAnchorPoint(CCPointZero);32 container->setPosition(CCPointZero);33 34 CCMenuItemImage *sp = CCMenuItemImage::create("HelloWorld.png", "HelloWorld.png",this,menu_selector(HelloWorld::buttonClick));35 sp->setPosition(VisibleRect::center());36 37 CCMenuItemImage *sp1 = CCMenuItemImage::create("HelloWorld1.png", "HelloWorld1.png",this,menu_selector(HelloWorld::buttonClick));38 sp1->setPosition(ccp(VisibleRect::center().x + VisibleRect::right().x,VisibleRect::center().y ));39 40 CCMenuItemImage *sp2 = CCMenuItemImage::create("HelloWorld2.png", "HelloWorld2.png",this,menu_selector(HelloWorld::buttonClick));41 sp2->setPosition(ccp(VisibleRect::center().x + VisibleRect::right().x * 2,VisibleRect::center().y ));42 43 CCMenu *menu2 = CCMenu::create(sp,sp1,sp2,NULL);44 menu2->setPosition(CCPointZero);45 menu2->setTouchPriority(2);46 47 container->addChild(menu2);48 container->setContentSize(CCSizeMake(640*3, 1136));//重要,默认是屏幕大小,可以去看源码49 CCLog("container contentsize:%f,%f",container->getContentSize().width,container->getContentSize().height);50 51 52 _scrollView = MyScrollView::create(CCSizeMake(640, 1136));53 //_scrollView->setContentSize();54 _scrollView->setDelegate(this);55 _scrollView->setTouchPriority(1);56 _scrollView->setContainer(container);57 _scrollView->setDirection(kCCScrollViewDirectionHorizontal);58 59 this->addChild(_scrollView);60 61 return true;62 }63 64 void HelloWorld::buttonClick(){65 float a = _scrollView->getContentOffset().x;66 if((int)a%640!=0){67 CCLog("**************************isDragging");68 }else{69 CCLog("##########################buttonClick#");70 }71 }72 73 void HelloWorld::scrollViewDidScroll(CCScrollView* view){74 CCLog("scrollViewDidScroll");75 }76 void HelloWorld::scrollViewDidZoom(CCScrollView* view){77 CCLog("scrollViewDidZoom");78 }
ScrollView和Menu的触摸事件优先级有点冲突,Menu的值低优先级高,如果不改的话在Menu拖动的话没反应,这应该不是我们想要的。
改了它的优先级之后,拖动可以了,但是拖动后也会触发Menu的点击。
!!!!!!!!肿么办...据说可以阻断事件的传播,但是我不会弄,谁要会请告诉我。!!!!!!!!
变相实现了一下,在按钮的事件中判断ScrollView是否滚动到位,不到位啥也不干。
另外,ScrollView的默认调整被关掉了,自己写了一下,默认的只会自动调整头尾。