void CAScrollView::closeToPoint(float delay) { CCSize size = this->getContentSize(); CCPoint point = m_pContainer->getFrameOrigin(); CCPoint resilience = ccpSub(m_tCloseToPoint, point); if (resilience.getLength() <= 0.5f) { m_pContainer->setFrameOrigin(m_tCloseToPoint); CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CAScrollView::closeToPoint), this); m_tCloseToPoint = CCPoint(-1, -1); } else { resilience.x /= size.width; resilience.y /= size.height; resilience = ccpMult(resilience, maxBouncesSpeed(delay)); resilience = ccpAdd(resilience, point); m_pContainer->setFrameOrigin(resilience); } }
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); } }