void CAScrollView::deaccelerateScrolling(float dt) { dt = MIN(dt, 1/30.0f); dt = MAX(dt, 1/100.0f); if (m_tInertia.getLength() > maxSpeedCache(dt)) { m_tInertia = ccpMult(m_tInertia, maxSpeedCache(dt) / m_tInertia.getLength()); } DPoint speed = DPointZero; if (m_tInertia.getLength() > maxSpeed(dt)) { speed = ccpMult(m_tInertia, maxSpeed(dt) / m_tInertia.getLength()); } else { speed = m_tInertia; } DPoint point = m_pContainer->getFrameOrigin(); if (m_bBounces) { DPoint resilience = DPointZero; DSize size = this->getBounds().size; DPoint curr_point = m_pContainer->getFrameOrigin(); DPoint relust_point = curr_point; this->getScrollWindowNotOutPoint(relust_point); float off_x = relust_point.x - curr_point.x; float off_y = relust_point.y - curr_point.y; if (fabsf(off_x) > FLT_EPSILON) { resilience.x = off_x / size.width; } if (fabsf(off_y) > FLT_EPSILON) { resilience.y = off_y / size.height; } resilience = ccpMult(resilience, maxSpeed(dt)); if (speed.getLength() < resilience.getLength()) { speed = resilience; m_tInertia = DPointZero; } } if (speed.getLength() < 0.25f) { m_tInertia = DPointZero; this->getScrollWindowNotOutPoint(point); this->setContainerFrame(point); this->update(0); this->hideIndicator(); this->stopDeaccelerateScroll(); if (m_pScrollViewDelegate) { m_pScrollViewDelegate->scrollViewStopMoved(this); } } else { point = ccpAdd(point, speed); if (this->isScrollWindowNotMaxOutSide(point)) { m_tInertia = DPointZero; } if (m_bBounces == false) { this->getScrollWindowNotOutPoint(point); } else { if (m_bBounceHorizontal == false) { point.x = this->getScrollWindowNotOutHorizontal(point.x); } if (m_bBounceVertical == false) { point.y = this->getScrollWindowNotOutVertical(point.y); } } if (point.equals(m_pContainer->getFrameOrigin())) { m_tInertia = DPointZero; } else { this->showIndicator(); this->setContainerFrame(point); } if (fabsf(m_tInertia.x) > 16) { m_tInertia.x = m_tInertia.x * (1 - decelerationRatio(dt)); } else if (fabsf(m_tInertia.x) > FLT_EPSILON) { m_tInertia.x = MAX((fabsf(m_tInertia.x) - 0.5f), 0) * fabsf(m_tInertia.x) / m_tInertia.x; } if (fabsf(m_tInertia.y) > 16) { m_tInertia.y = m_tInertia.y * (1 - decelerationRatio(dt)); } else if (fabsf(m_tInertia.y) > FLT_EPSILON) { m_tInertia.y = MAX((fabsf(m_tInertia.y) - 0.5f), 0) * fabsf(m_tInertia.y) / m_tInertia.y; } if (m_pScrollViewDelegate) { m_pScrollViewDelegate->scrollViewDidMoved(this); } } }
void CAScrollView::deaccelerateScrolling(float delay) { if (m_tInertia.getLength() > maxSpeedCache(delay)) { m_tInertia = ccpMult(m_tInertia, maxSpeedCache(delay) / m_tInertia.getLength()); } CCPoint speed; if (m_tInertia.getLength() > maxSpeed(delay)) { speed = ccpMult(m_tInertia, maxSpeed(delay) / m_tInertia.getLength()); } else { speed = m_tInertia; } m_tInertia = ccpMult(m_tInertia, 1 - decelerationRatio(delay)); CCPoint point = m_pContainer->getCenterOrigin(); if (m_bBounces) { CCSize size = this->getContentSize(); CCSize cSize = m_pContainer->getFrame().size; cSize.width = MAX(cSize.width, size.width); cSize.height = MAX(cSize.height, size.height); CCPoint resilience = CCPointZero; if (point.x - cSize.width / 2 > 0) { resilience.x = (point.x - cSize.width / 2) / size.width; } if (point.y - cSize.height / 2 > 0) { resilience.y = (point.y - cSize.height / 2) / size.height; } if ((point.x + cSize.width / 2 - size.width) < 0) { resilience.x = (point.x + cSize.width / 2 - size.width) / size.width; } if ((point.y + cSize.height / 2 - size.height) < 0) { resilience.y = (point.y + cSize.height / 2 - size.height) / size.height; } resilience = ccpMult(resilience, maxBouncesSpeed(delay)); if (speed.getLength() < resilience.getLength()) { speed = ccpSub(speed, resilience); m_tInertia = CCPointZero; } } point = ccpAdd(point, speed); if (this->isScrollWindowNotMaxOutSide()) { m_tInertia = CCPointZero; } if (m_bBounces == false) { point = this->getScrollWindowNotOutPoint(point); } else { if (m_bBounceHorizontal == false) { point.x = this->getScrollWindowNotOutHorizontal(point.x); } if (m_bBounceVertical == false) { point.y = this->getScrollWindowNotOutVertical(point.y); } } if (m_pScrollViewDelegate && point.equals(m_pContainer->getCenter().origin) == false) { m_pScrollViewDelegate->scrollViewDidScroll(this); } m_bDecelerating = true; if (speed.getLength() <= 0.5f) { point = this->getScrollWindowNotOutPoint(point); m_bDecelerating = false; CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CAScrollView::deaccelerateScrolling), this); } m_pContainer->setCenterOrigin(point); }
void CAScrollView::deaccelerateScrolling(float dt) { dt = MIN(dt, 1/30.0f); dt = MAX(dt, 1/100.0f); if (m_tInertia.getLength() > maxSpeedCache(dt)) { m_tInertia = ccpMult(m_tInertia, maxSpeedCache(dt) / m_tInertia.getLength()); } CCPoint speed = CCPointZero; if (m_tInertia.getLength() > maxSpeed(dt)) { speed = ccpMult(m_tInertia, maxSpeed(dt) / m_tInertia.getLength()); } else { speed = m_tInertia; } if (!m_tInertia.equals(CCPointZero)) { m_tInertia = ccpMult(m_tInertia, 1 - decelerationRatio(dt)); } CCPoint point = m_pContainer->getFrameOrigin(); if (m_bBounces) { CCSize size = this->getBounds().size; CCSize cSize = m_pContainer->getFrame().size; cSize.width = MAX(cSize.width, size.width); cSize.height = MAX(cSize.height, size.height); float y_max = this->isHeaderRefreshing() ? _px(128) : 0.0f; float y_min = this->isFooterRefreshing() ? size.height - cSize.height - _px(128) : size.height - cSize.height; CCPoint resilience = CCPointZero; if (point.x > 0) { resilience.x = (point.x) / size.width; } if (point.y - y_max > 0) { resilience.y = (point.y - y_max) / size.height; } if ((point.x + cSize.width - size.width) < 0) { resilience.x = (point.x + cSize.width - size.width) / size.width; } if ((point.y - y_min) < 0) { resilience.y = (point.y - y_min) / size.height; } resilience = ccpMult(resilience, maxBouncesSpeed(dt)); if (speed.getLength() < resilience.getLength()) { speed = ccpMult(resilience, -1.0f); m_tInertia = CCPointZero; } } if (speed.getLength() <= minSpeed(dt) / 2) { point = this->getScrollWindowNotOutPoint(point); this->setContainerFrame(point); this->hideIndicator(); //this->detectionFromPullToRefreshView(); this->stopDeaccelerateScroll(); } else { point = ccpAdd(point, speed); if (this->isScrollWindowNotMaxOutSide(m_pContainer->getFrameOrigin())) { m_tInertia = CCPointZero; } if (m_bBounces == false) { point = this->getScrollWindowNotOutPoint(point); } else { if (m_bBounceHorizontal == false) { point.x = this->getScrollWindowNotOutHorizontal(point.x); } if (m_bBounceVertical == false) { point.y = this->getScrollWindowNotOutVertical(point.y); } } //this->changedFromPullToRefreshView(); this->showIndicator(); this->setContainerFrame(point); } if (m_pScrollViewDelegate) { m_pScrollViewDelegate->scrollViewDidMoved(this); } }