void MarketShopListLayer::registerWithTouchDispatcher(){
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);//优先级设为0,就不会出现按到按钮,不能进行滑动
}
bool MarketShopListLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
startPoint=this->convertTouchToNodeSpace(pTouch);
starTime=millisecondNow();
return true;
}
void MarketShopListLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
CCPoint movePoint=this->convertTouchToNodeSpace(pTouch);
CCPoint disPoint= ccpSub(movePoint, startPoint);
float disX=disPoint.x;
float disY=disPoint.y;
if (fabsf(disX)>fabsf(disY)) {
leftRight=true;
tableView->setTouchEnabled(false);//左右滑动
tableView->setPosition(ccp(tbPoint.x+disX,tbPoint.y));
}else{
leftRight=false;
tableView->setTouchEnabled(true);//上下滑动
}
// ******************************************
bool isDragingDown=movePoint.y<startPoint.y;
float difference=fabsf(movePoint.y-startPoint.y);
if (isRefreshShow&&isDragingDown&&(difference>420)) {
CCLog(“refresh”);
((SwitchView *)_delegate)->postNotificationName(MARKET_SHOP_LISTC, CCInteger::create(_currentPage));
}
}
void MarketShopListLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
if (leftRight) {
CCPoint endPoint=this->convertTouchToNodeSpace(pTouch);
float dis=fabsf(ccpSub(endPoint, startPoint).x);
long endTime=millisecondNow();
std::cout<<”差值:”<<fabs(starTime-endTime)<<std::endl;
if (dis>=tableView->getContentSize().width/2||(fabs(starTime-endTime)>49)) {
if (dis>0) {//向右
_currentPage—;
_currentPage=MAX(_currentPage, 1);
}else{//向左
_currentPage++;
_currentPage=MIN(_currentPage, _totalPage);
}
((SwitchView*)_delegate)->postNotificationName(MARKET_SHOP_LISTC, CCInteger::create(_currentPage));
pageLabel->setString(CCString::createWithFormat(“%d/%d”,_currentPage,_totalPage)->getCString());
}
}
tableView->runAction(CCMoveTo::create(0.5,tbPoint));
}
//****************************************************************************
long MarketShopListLayer::millisecondNow(){
struct cc_timeval now;
CCTime::gettimeofdayCocos2d(&now, NULL);
return (now.tv_sec * 1000 + now.tv_usec / 1000);
}
void MarketShopListLayer::setDelegate(CCObject* delegate){
_delegate=delegate;
CC_SAFE_RETAIN(_delegate);
}