MichiganLabs / AnimatingTableViewHeader

A Swift project explaining how to animate a header above a UITableView
164 stars 35 forks source link

isScrollingDown and isScrollingUp - appear in revserse #6

Closed ashish-naik closed 6 years ago

ashish-naik commented 6 years ago

When i scroll down, isScrollingDown derives as false and if i scroll up then isScrollingUp is false.

why this opposite calculation?

I have UISearchBar in place UIView.

when i scroll up, scrollView.contentOffset.y gives positive value. Should it not give negative?

Thanks Ashish

func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let scrollDiff = scrollView.contentOffset.y - self.previousScrollOffset

        let absoluteTop: CGFloat = 0;
        let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height;

        let isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop
        let isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom

        var newHeight = self.searchBarHeightConstraint.constant

        if isScrollingDown {
            newHeight = max(self.minHeaderHeight, self.searchBarHeightConstraint.constant - abs(scrollDiff))

        } else if isScrollingUp {
            newHeight = min(self.maxHeaderHeight, self.searchBarHeightConstraint.constant + abs(scrollDiff))

            newSearchBar.showsScopeBar = false

        }

      if newHeight != self.searchBarHeightConstraint.constant {
            self.searchBarHeightConstraint.constant = newHeight
            self.setScrollPosition(position: self.previousScrollOffset)

        }

        self.previousScrollOffset = scrollView.contentOffset.y

{
johndelong commented 6 years ago

@ashish-naik Hmm, that is peculiar. Your code looks correct. In-fact, I've copied it over into my project and it seems to operate as I would expect.

Is it perhaps that we have opposite interpretations of what scroll directions are? For me when I am "scrolling down", I drag up with my finger (or mouse when on a simulator) so that I can view more content downward on the page. Vice versa when I am "scrolling up". The biggest tell tale is the scroll bar on the right hand side. If the scroll bar is going down, you are scrolling down. If it is going up, you are scrolling up. This scroll direction interpretation is pretty typical.

Let me know if you are seeing something different than what I've described above and in this screen recording below.

scroll_direction

ashish-naik commented 6 years ago

I get it now and feel so ashamed I didn't thought the natural way !!!

Will check again. Thanks.

johndelong commented 6 years ago

No problem! Glad we were able to get to the bottom of it! 👍

ashish-naik commented 6 years ago

yes it working fine. Thanks.