From 10cfcec77662b65867f864dc0b34225918c5f294 Mon Sep 17 00:00:00 2001 From: Daniil Vinogradov Date: Fri, 19 Mar 2021 21:07:09 +0300 Subject: [PATCH] Simplify scrolling limits --- library/lib/views/scrolling_frame.cpp | 56 ++++++++------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/library/lib/views/scrolling_frame.cpp b/library/lib/views/scrolling_frame.cpp index 722463b9..c772a1b1 100644 --- a/library/lib/views/scrolling_frame.cpp +++ b/library/lib/views/scrolling_frame.cpp @@ -41,54 +41,16 @@ ScrollingFrame::ScrollingFrame() if (state.state == GestureState::START) startY = this->scrollY * contentHeight; - float newScroll = (startY - (state.position.y - state.startPosition.y)) / contentHeight; - float bottomLimit = (contentHeight - this->getScrollingAreaHeight()) / contentHeight; - - // Bottom boundary - if (newScroll > bottomLimit) - newScroll = bottomLimit + (newScroll - bottomLimit) / 5.0f; - - // Top boundary - if (newScroll < 0.0f) - newScroll = newScroll / 5.0f; + float newScroll = (startY - (state.position.y - state.startPosition.y)) / contentHeight; // Start animation if (state.state != GestureState::END) startScrolling(false, newScroll); else { - if (this->scrollY < 0) - { - startScrolling(true, 0); - return; - } - - float bottomLimit = contentHeight - this->getScrollingAreaHeight(); - float bottomLimitNormal = bottomLimit / contentHeight; - - if (this->scrollY > bottomLimitNormal) - { - startScrolling(true, bottomLimitNormal); - return; - } - float time = state.acceleration.time.y * 1000.0f; float newPos = this->scrollY * contentHeight + state.acceleration.distance.y; - // Bottom boundary - if (newPos > bottomLimit) - { - time = time * (1 - fabs(newPos - bottomLimit) / fabs(state.acceleration.distance.y)); - newPos = bottomLimit; - } - - // Top boundary - if (newPos < 0) - { - time = time * (1 - fabs(newPos) / fabs(state.acceleration.distance.y)); - newPos = 0; - } - newScroll = newPos / contentHeight; if (newScroll == this->scrollY || time < 100) @@ -249,7 +211,21 @@ float ScrollingFrame::getContentHeight() void ScrollingFrame::scrollAnimationTick() { if (this->contentView) - this->contentView->setTranslationY(-(this->scrollY * this->getContentHeight())); + { + float contentHeight = this->getContentHeight(); + float bottomLimit = (contentHeight - this->getScrollingAreaHeight()) / contentHeight; + + if (this->scrollY < 0) + this->scrollY = 0; + + if (this->scrollY > bottomLimit) + this->scrollY = bottomLimit; + + if (contentHeight <= getHeight()) + this->scrollY = 0; + + this->contentView->setTranslationY(-(this->scrollY * contentHeight)); + } } void ScrollingFrame::onChildFocusGained(View* directChild, View* focusedView)