简体   繁体   English

将“收藏夹”视图滚动到顶部

[英]Scroll Collection view to the top

I have a following collection view. 我有以下收藏视图。 When the user scrolls the view up, I want the collection view to go all the way up. 当用户向上滚动视图时,我希望集合视图一直向上滚动。 Similarly, when it is scrolled down, I want it to come back to its original position. 同样,当它向下滚动时,我希望它回到其原始位置。

I have a uiview and imageView above the collectionview. 我在collectionview上方有一个uiview和imageView。

在此处输入图片说明

Now, If this was a tableview, I would have used header. 现在,如果这是一个表视图,我将使用标头。 How do i achieve this behavious. 我如何实现这种行为。 I am out of Ideas. 我没主意了。

Thanks in advance 提前致谢

UICollectionView is the sub-class of UIScrollView so you can achieve what you want is just by the scrollview delegate method and the constraints of your UIView and UIImageView upon the UICollectionView. UICollectionViewUIScrollView的子类,因此您可以仅通过scrollview delegate方法以及UICollectionView上的UIViewUIImageView约束来实现所需的功能。

Here I am going to give you one example to achieve this with scrollview delegate. 在这里,我将为您提供一个示例,以使用scrollview委托实现此目的。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == collectionView{
            if scrollView.contentOffset.y == 0{
                UIView.animate(withDuration: 0.5) { () -> Void in
                    self.constTopTagHeight.constant = 175 // Your UIView and imageview constraints adjust.  
                    self.view.layoutIfNeeded()
                }
            }
            else{
                UIView.animate(withDuration: 0.5) { () -> Void in
                    self.constTopTagHeight.constant = 0 // Your UIView and imageview constraints adjust.
                    self.view.layoutIfNeeded()
                }
            }
        }
    }

Hope this help you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM