bool isTouching;//是否在正在触摸屏幕
bool isRoll;//是否正在滚动
bool isSectionChange;//是否改变了选项
bool isClick;//是否点击该选项
float oldPX;//旧坐标
float newPX;//新坐标
float speedDown;//减速度
void MainScene::update(float dt)
{
this->isRoll = false;
this->newPX =this->scrollView->getInnerContainer()->getPosition().x;
if(int(this->scrollView->getInnerContainer()->getPosition().x) == 0 ||//如果在头尾,速度设为原来的值
int(this->scrollView->getInnerContainer()->getPosition().x) == -((400+SPACE)*5.3-scrollView->getSize().width))
{
speedDown = 6;
isTouching = false;
}
if(isTouching == false)
{
//各个选项的位置
int A = -140;
int B = A - guankaLast1->getSize().width-SPACE;
int C = B - guankaLast1->getSize().width-SPACE;
int D= C - guankaLast1->getSize().width-SPACE;
int E= D - guankaLast1->getSize().width-SPACE;
//选项数组
int array[] = {A, B, C, D, E};
//头部回弹
if(int(this->scrollView->getInnerContainer()->getPosition().x)>array[0])
{
this->isRoll = true;
isSectionChange = true;
this->scrollView->scrollChildren(-speedDown);
speedDown-=0.1;
if(speedDown<1)
{
speedDown = 1;
}
}
//中间逻辑
for (int i = 0; i<5; i++)
{
if(int(this->scrollView->getInnerContainer()->getPosition().x)<array[i]-3 &&
int(this->scrollView->getInnerContainer()->getPosition().x)>array[i+1]+3)
{
this->isRoll = true;
isSectionChange = true;
if(this->newPX<this->oldPX)
{
CCLog("old>new");
this->scrollView->scrollChildren(-speedDown);
}
else
{
CCLog("old<new");
this->scrollView->scrollChildren(speedDown);
}
speedDown -= 0.01;
if (speedDown<1)
{
speedDown = 1;
}
}
}
//尾部回弹
if(int(this->scrollView->getInnerContainer()->getPosition().x)<array[4] )
{
this->isRoll = true;
isSectionChange = true;
this->scrollView->scrollChildren(speedDown);
speedDown-=0.1;
if(speedDown<1)
{
speedDown = 1;
}
}
//如果在滚动
if(this->isRoll)
{
this->rotateLight->setVisible(false);
}
//判断停止位置
if (this->isRoll == false && isSectionChange == true)
{
isSectionChange = false;
for (int i = 0; i < 5; i++)
{
if (int(this->scrollView->getInnerContainer()->getPosition().x) <= array[i]+3 &&
int(this->scrollView->getInnerContainer()->getPosition().x) >= array[i]-3)
{
this->selectSection(i);
if (GDI->isSectionCanOpened(i))
{
this->rotateLight->setVisible(true);
}
}
}
}
}
}
//滑动容器模块
UIImageView* background = (UIImageView*) this->mainLayer->getWidgetByTag(121);
backgoundSize =background->getContentSize();
backgoundPosition =background->getPosition();
scrollView = UIScrollView::create();
scrollView->setDirection(SCROLLVIEW_DIR_HORIZONTAL);
scrollView->setTouchEnable(true);
scrollView->setSize(backgoundSize-CCSize(25,0));
scrollView->setInnerContainerSize(CCSize( (400+SPACE)*5.3, scrollView->getSize().height));
scrollView->setPosition(backgoundPosition-CCPoint(backgoundSize.width/2, backgoundSize.height/2)+CCPoint(5,0));
this->scrollView->addTouchEventListener(this, toucheventselector(MainScene::pageViewEvent));
this->mainLayer->addWidget(this->scrollView);
Layout* layout = Layout::create();
layout->setSize(backgoundSize);
guankaLast2->setPosition(CCPoint(guankaLast1->getPosition().x+guankaLast1->getSize().width+SPACE,backgoundPosition.y-330));
layout->addChild(guankaLast2);
scrollView->addChild(layout);
{
this->guankaLast2->setScaleX(1.5);
this->guanka2Ani->setScaleX(1 / 1.5);
this->guanka2Bor->setScaleX(1 / 1.5);
}
//button按钮事件
switch (type)
{
case cocos2d::extension::TOUCH_EVENT_BEGAN:
this->isTouching = true;
this->isClick = true;
this->oldPX = this->scrollView->getInnerContainer()->getPosition().x;
this->speedDown = 6;
CCLog("began");
break;
case cocos2d::extension::TOUCH_EVENT_MOVED:
this->isClick = false;
CCLog("move");
break;
case cocos2d::extension::TOUCH_EVENT_ENDED:
if (isClick)
{
}
this->isTouching =false;
CCLog("ended");
break;
case cocos2d::extension::TOUCH_EVENT_CANCELED:
this->isTouching =false;
CCLog("canceled");
break;
default:
break;
}