header disappears by decreasing its height when scrolled up

29 views Asked by At

I would like When you scroll, it does not move downwards by decreasing the height on collectionview.

I want it in my application, just like in YouTube's iOS application, where the header disappears by decreasing its height when scrolled up.you can see on video too video:

https://youtu.be/Raz69tHhKdI

I wrote code like this

extension TemplatesViewController{
    func showTagsPlaceHolder() {
        DispatchQueue.main.async { [self] in
            
            tagsPlaceHolderViewHeightAnchor.isActive = false
            tagsPlaceHolderViewHeightAnchor.constant = 120
            tagsPlaceHolderViewHeightAnchor.isActive = true
            //tagsPlaceholder.isHidden = false
          
            
            self.view.setNeedsLayout()
            UIView.animate(withDuration: 0.5) {
                tagsPlaceholder.alpha = 1
                self.view.layoutIfNeeded()
            }
            
        }
      
        
    }
    func hideTagsPlaceHolder() {
        DispatchQueue.main.async {[self] in
            
            tagsPlaceHolderViewHeightAnchor.isActive = false
            tagsPlaceHolderViewHeightAnchor.constant = 0
            tagsPlaceHolderViewHeightAnchor.isActive = true
            
            self.view.setNeedsLayout()
            UIView.animate(withDuration: 0.5) {
                tagsPlaceholder.alpha = 0
                self.view.layoutIfNeeded()
            }
            
        }
       
    }
    
   
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let yOffset = scrollView.contentOffset.y
        let maxOffsetY = scrollView.contentSize.height - scrollView.frame.size.height;
        
        //refresh the table view but disable the bounce
        if yOffset < 0.0 || yOffset > maxOffsetY{
            return
         }
     
         if (self.lastContentOffset > yOffset) {
              // move up
              print("move up")
              showTagsPlaceHolder()
              //tagsPlaceHolderIsHidden = true
              
          }
          else if (self.lastContentOffset < yOffset) {
              // move down
              print("move down")
              hideTagsPlaceHolder()
              //tagsPlaceHolderIsHidden = false
             
          }
          self.lastContentOffset = yOffset
        
       }
    
}

The only problem is that when you scroll up, the height gradually decreases, or when you scroll down, the height gradually increases. It should be smooth like YouTube. also I am sharing project : project files

0

There are 0 answers