cocos2dx ScrollView 測試一 觸摸事件優先級和自動調整


直接上代碼

MyScrollView.h

 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__) */
View Code

MyScrollView.cpp

 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     else
20     {
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     else
34     {
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 }
View Code

HelloWorldScene.h

 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 CCScrollViewDelegate
12 {
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__
View Code

HelloWorldScene.cpp

 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 instance
14 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 }
View Code

 

ScrollView和Menu的觸摸事件優先級有點沖突,Menu的值低優先級高,如果不改的話在Menu拖動的話沒反應,這應該不是我們想要的。

改了它的優先級之后,拖動可以了,但是拖動后也會觸發Menu的點擊。

!!!!!!!!腫么辦...據說可以阻斷事件的傳播,但是我不會弄,誰要會請告訴我。!!!!!!!!

變相實現了一下,在按鈕的事件中判斷ScrollView是否滾動到位,不到位啥也不干。

 

另外,ScrollView的默認調整被關掉了,自己寫了一下,默認的只會自動調整頭尾。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM