使用UIViews在顶部滚动集合视图?

时间:2016-08-12 19:47:20

标签: ios swift uiview uiscrollview uicollectionview

我有一个UICollectionViewController,我添加了两个UIViews作为子视图。一个是紫色的UIView,上面是另一个白色的UIView,蓝色的集合视图在两个后面都向上滚动。

在UIViews之下,我从前300名({2}的总高度中得到collectionView.contentInset。我想要完成的是滚动集合视图以及上面的两个UIViews。它与此线程(Move a view when scrolling in UITableView)上的解决方案几乎相似,除非我覆盖scrollViewDidScroll,整个框架向上滚动,单元格在两个视图后面。我想要的只是向上滚动UIViews,然后滚动浏览集合视图。我觉得这可能涉及嵌套的滚动视图。

enter image description here

这就是我覆盖scrollViewDidScroll的方式:

    var rect = self.view.frame
    rect.origin.y =  -scrollView.contentOffset.y - 300
    self.view.frame = rect

编辑:我发布了一个视频,演示了我想要为每个iOS Tumblr应用做些什么:https://youtu.be/elfxtzoiHQo

4 个答案:

答案 0 :(得分:3)

我通过以下基本步骤达到了相同的要求。

//Declare the view which is going to be added as header view
    let requiredView = UIView()

//Add your required view as subview of uicollectionview backgroundView view like as
collectionView.backgroundView = UIView() 
collectionView.backgroundView?.addSubview(requiredView)

//After that control the frame of requiredHeaderView in scrollViewDidScroll delegate method like
        func scrollViewDidScroll(scrollView: UIScrollView) {
         let per:CGFloat = 60 //percentage of required view to move on while moving collection view
                let deductValue = CGFloat(per / 100 * requiredView.frame.size.height)
                let offset = (-(per/100)) * (scrollView.contentOffset.y)
                let value = offset - deductValue
                let rect = requiredView.frame
                self.requiredView.frame = CGRectMake(rect.origin.x, value, rect.size.width, rect.size.height)
        }

答案 1 :(得分:1)

听起来你想要的是标题。

您可以使用以下任一项为标题指定类或nib:

self.collectionView.registerClass(_:, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier:)

registerNib(_:, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: )

如果您使用的是流式布局,还应指定参考高度:self.flowLayout.headerReferenceHeight = ...

然后你可以通过你的UICollectionViewController在collectionView(_:, viewForSupplementaryElementOfKind:, at:)中通过检查节标题类型提供标题。

以下是一个体面的教程供参考:https://www.raywenderlich.com/78551/beginning-ios-collection-views-swift-part-2

答案 2 :(得分:0)

您有一个图书馆CSStickyHeaderFlowLayout

来自自述文件:

  

UICollectionView替换UITableView。甚至更像Parallax Header,Sticky Section Header。适用于iOS 7。

答案 3 :(得分:0)

试试这个。

headerViewYConstraint是标题视图的前y约束。

存储最后一次接触偏移。

var lastContentOffset: CGFloat = 0


override func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView != contentScrollView {

        if scrollView.dragging || scrollView.decelerating {
            let newOffset = scrollView.contentOffset.y
            let headerViewHeight = headerView.frame.width
            if headerViewHeight > 0 && scrollView.contentSize.height > view.frame.height + headerViewHeight{
                var topOffset = newOffset == 0 ? 0.0 : (headerViewYConstraint.constant + lastContentOffset - newOffset)
                topOffset = min(0, max(topOffset, -headerViewHeight))
                if headerViewYConstraint.constant > topOffset || newOffset < headerViewHeight || lastDirectionalContentOffset - newOffset > cellHeight(){
                    headerViewYConstraint.constant = topOffset
                }
            } else {
                headerViewYConstraint.constant = 0
            }
            lastContentOffset = newOffset
        }
    }
}