Code Monkey home page Code Monkey logo

Comments (6)

lordcodes avatar lordcodes commented on August 24, 2024 1

I needed it to work in this way and used a different solution.

In the canAnimateHeader method, I just checked if current scroll position is greater than the point you want it to remain collapsed. If it is then just do nothing at all, if it isn't then, animate the header.

private func canAnimateHeader(forScrollView scrollView: UIScrollView) -> Bool {
        if scrollView.contentOffset.y >= alwaysCollapsedScrollOffset {
            return false
        }
        // isContentToScroll is same as previous implementation, 
        // I just broke it up a little
        return isContentToScroll(forScrollView: scrollView)
    }

    private func isContentToScroll(forScrollView scrollView: UIScrollView) -> Bool {
        let headerSize = headerHeight - headerHeightMinimum
        let scrollViewMaxHeight = scrollView.frame.height + headerSize
        return scrollView.contentSize.height > scrollViewMaxHeight
    }

from animatingtableviewheader.

kylebrowning avatar kylebrowning commented on August 24, 2024

Figured this out.

from animatingtableviewheader.

escully27 avatar escully27 commented on August 24, 2024

How did you do it?

from animatingtableviewheader.

kylebrowning avatar kylebrowning commented on August 24, 2024

@escully27 Sorry misunderstood your question.

I did it like this.

First set a scrolling position.

      if isScrollingDown {
            currentScrollingDirection = ScrollingDirection.down
            self.navigationController?.setNavigationBarHidden(true, animated: true)
        } else if isScrollingUp {
            currentScrollingDirection = ScrollingDirection.up
        }

Then,

        if scrollView.contentOffset.y <= 20 {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }


        if canAnimateHeader(scrollView) {
            self.previousScrollOffset = scrollView.contentOffset.y


            // Dont do anything if were not above a certain height and we are scrolling up
            if scrollView.contentOffset.y >= 120 && currentScrollingDirection == ScrollingDirection.up {
                return
            }

            // Calculate new header height
            let newHeight = self.getNewConstraintValue(constraint: self.extendedNavBarHeightConstraint, valueMin: self.minHeaderHeight, valueMax: self.maxHeaderHeight, scrollDiff: scrollDiff)
            
            // Header needs to animate
            if newHeight != self.extendedNavBarHeightConstraint.constant {
                self.extendedNavBarHeightConstraint.constant = newHeight
                self.setScrollPosition(self.previousScrollOffset)
            }
            
            self.emailLabelWidthConstraint.constant = self.getNewConstraintValue(constraint: self.emailLabelWidthConstraint, valueMin: self.minEmailWidth, valueMax: self.maxEmailWidth, scrollDiff: scrollDiff)
            self.emailLabelHeightConstraint.constant = self.getNewConstraintValue(constraint: self.emailLabelHeightConstraint, valueMin: self.minEmailHeight, valueMax: self.maxEmailHeight, scrollDiff: scrollDiff)
            self.imageViewWidthConstraint.constant = self.getNewConstraintValue(constraint: self.imageViewWidthConstraint, valueMin: self.minImageViewWidth, valueMax: self.maxImageViewWidth, scrollDiff: scrollDiff)
            self.imageViewHeightConstraint.constant = self.getNewConstraintValue(constraint: self.imageViewHeightConstraint, valueMin: self.minImageViewHeight, valueMax: self.maxImageViewHeight, scrollDiff: scrollDiff)
            self.supportTopConstraint.constant = self.getNewConstraintValue(constraint: self.supportTopConstraint, valueMin: self.minSupportTop, valueMax: self.maxSupportTop, scrollDiff: scrollDiff)
            self.emailTopConstraint.constant = self.getNewConstraintValue(constraint: self.emailTopConstraint, valueMin: self.minEmailTop, valueMax: self.maxEmailTop, scrollDiff: scrollDiff)
        }

from animatingtableviewheader.

kylebrowning avatar kylebrowning commented on August 24, 2024

I wrote a utilty function as well called getNewConstraintValue

    func getNewConstraintValue(constraint: NSLayoutConstraint, valueMin: CGFloat, valueMax: CGFloat, scrollDiff: CGFloat) -> CGFloat {
        switch currentScrollingDirection {
        case .down:
            return max(valueMin, constraint.constant - abs(scrollDiff))
        case .up:
            return min(valueMax, constraint.constant + abs(scrollDiff))
        case .none:
            return constraint.constant
        }
    }

from animatingtableviewheader.

escully27 avatar escully27 commented on August 24, 2024

Thanks for the response. Not working for me.. I have no navigation bar involved so it's behaving in a worse way than original. Cheers

from animatingtableviewheader.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.