http://discuss.cocos2d-x.org/t/cocos2d-ui-scrollview-and-sprite-why-not-function-the-ontouchbegan/21790
Add touch event listener to scrollview.
scrollview->addTouchEventListener(CC_CALLBACK_2(InApp::touchEvent, this));
void InApp::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
case Widget::TouchEventType::MOVED:
case Widget::TouchEventType::ENDED:
{
}
break;
case Widget::TouchEventType::CANCELED:
default:
break;
}
}
//////
取得偏移量
CCLOG("BEGAN");
auto player =dynamic_cast<Layer*>(getNode(PG_MAINSTDETIAIL, MINDETI_LAYER_HISTORY));
int layerViewH=player->getContentSize().height;
auto pScroll = static_cast<ui::ScrollView *>(pSender);
//取得點選座標
Point touchPoint=pScroll->getTouchBeganPosition();
//找出那一張圖被點選
//取得圖檔高度
Sprite *pSpriteImage=Sprite::createWithSpriteFrameName(S_MAIN_HISTORY_BK);
int imageHeight=pSpriteImage->getContentSize().height;
int offsetY=pScroll->getInnerContainerPosition().y;
//計算卷軸移動量(0 ~ -616)
//int offsety2=pScroll->getPositionPercent().y;
//計算整個總高度(1500)
int scrollHeight=pScroll->getInnerContainerSize().height;
//計算點選座標且換到圖檔位置
int iHitY = touchPoint.y+(0-offsetY);
int iOrd=iHitY/imageHeight;
//根據編號取得產品圖檔
auto pSpriteName = getErrStatusProductImageName(iOrd);
auto pSprite =dynamic_cast<Sprite*>(getNode(PG_MAINSTDETIAIL, MINDETI_SPRITE_ICON));
auto pFrame= SpriteFrameCache::getInstance()->getSpriteFrameByName(pSpriteName);
pSprite->setSpriteFrame(pFrame);