How to implement touchevent in pageview

When i use pageView in Scene. touch event is doesn’t work.

I was write this.

PageView* pageView = PageView::create();
pageView->setTouchEnabled(true);
pageView->setSize(Size(winSize.width,winSize.height));
//pageView->setAnchorPoint(Point(0,0));
//pageView->setPosition(Point(winSize.width/2,winSize.height/2));

for (int i = 0; i < 7; i++) {
    int position = i + 1;
    int tag = i + 100;
    char res[15]={0};
    sprintf(res, "continent%d.png",position);
    auto layout = Layout::create();
    layout->setSize(winSize);
    Sprite* imageView = Sprite::create(img_continent[i]);
    imageView->setPosition(Point(winSize.width/2,winSize.height/2));
    imageView->setTag(tag);
    imageView->setScale(0.87);
     EventDispatcher* dispatcher1 = Director::getInstance()->getEventDispatcher();
     _touchListener = EventListenerTouchOneByOne::create();
     _touchListener->setSwallowTouches(true);
     _touchListener->onTouchBegan = CC_CALLBACK_2(SelectContinent::onTouchBegan, this);
     dispatcher1->addEventListenerWithSceneGraphPriority( _touchListener, imageView);
    continent_img.pushBack(imageView);
    layout->addChild(imageView);
    pageView->addPage(layout);
}
this->addChild(pageView);
pageView->addEventListenerPageView(this, pagevieweventselector(SelectContinent::pageviewCallBack));

I want occur touch event only sprite(imageView). but that code is dosen’t work touch event.
and when i chage page or touch page, called pageviewCallBack.

how to implement that?

Try removing dispatcher part from your loop

Thanks for your reply. but until don’t work.

i was change code

EventDispatcher* dispatcher1 = Director::getInstance()->getEventDispatcher();

for(){
     imageView......
     _touchListener....
     dispatcher1->addEventListenerWithSceneGraphPriority(_touchListener,imageView);
}
  1. I am not a master of gui but I think you should not use sprite since there is “ImageView” widget. It is gui component that displays texture. Then you don’t have to create listener for Sprite!.
  2. If you create a listener for a node that is child of other node then if you touch on the area where child and parent are visible then the touch is detected only on CHILD as far as I know. Touches are only detected on node “on top”.
    3.Btw. Why would you need to detect when the sprite is touched?
    4.Read test (cocos2dxEngineRoot\tests\cpp-tests\Classes\UITest\CocoStudioGUITest\UIPageViewTest\UIPageViewTest.cpp

Try something like that:

PageView* pageView = PageView::create();
pageView->setSize(Size(winSize.width,winSize.height));

for (int i = 0; i < 7; i++) {
    int position = i + 1;
    int tag = i + 100;
    char res[15]={0};
    sprintf(res, "continent%d.png",position);
auto layout = Layout::create();
    layout->setSize(winSize);
ImageView *imageView = ImageView::create(img_continent[i]);
imageView->setPosition(Point(winSize.width/2,winSize.height/2));
    imageView->setTag(tag);
    imageView->setScale(0.87);
continent_img.pushBack(imageView);   //I guess this is vector for getting Widgets in page listener right?
    layout->addChild(imageView);
    pageView->addPage(layout);
   }
pageView->addEventListener([&continent_img,pageView](cocos2d::Ref* pSender, cocos2d::ui::PageView::EventType type)
{
     if (type != PageView::EventType::TURNING) return;
     continent_img.at(pageView->getCurPageIndex())->setOpacity(50); //or something else you want to do to //selected image at pageView 
});

that is occur error.

My purpose is distinction tab page and swipe page.
ex) I have 5 page. and i will chose one. then i tap the sprite in page, app will change scene.