如何计算collectionView部分之间的间隔

时间:2015-11-16 16:03:27

标签: ios swift uicollectionview

如何计算UICollectionView Section或Header帧之间的间隔。我有自定义PhotoViewFlowLayout它滚动浏览集合视图时工作得很好,但headerView我的headerView问题显示错误的地方。

override func prepareLayout() {
        super.prepareLayout()
        let columnWidth:CGFloat = contentWidth / numberOfColumns
        if cache.isEmpty {
            var column = 0
            var row = 0
            if let numberOfSections = collectionView?.numberOfSections() {
                for index in 0 ..< numberOfSections {
                    let indexPath = NSIndexPath(forItem: 0, inSection: index)
                    stickyHeaderIndexPaths.append(indexPath)

                    for item in 0 ..< collectionView!.numberOfItemsInSection(0) {
                        let indexPath = NSIndexPath(forItem: item, inSection: 0)

                        ...

                        let frame = CGRect(x: cell_x, y: cell_y, width: cell_size, height: cell_size)
                        let insetFrame = CGRectInset(frame, cellPadding, cellPadding)

                        let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
                        attributes.frame = insetFrame
                        cache.append(attributes)
                    }
                }
            }
            contentHeight = CGFloat(row) * CGFloat(columnWidth - cellPadding*2)
        }
    }

    override func collectionViewContentSize() -> CGSize {

        return CGSize(width: contentWidth, height: contentHeight)
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        var layoutAttributes = [UICollectionViewLayoutAttributes]()

        for attributes in cache {
            if CGRectIntersectsRect(attributes.frame, rect) {
                layoutAttributes.append(attributes)
            }
        }
        for indexPath in stickyHeaderIndexPaths {
            let headerAttribute = layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath)
            print(headerAttribute)

            layoutAttributes.append(headerAttribute!)
        }
        return layoutAttributes
    }


    override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let layoutAttribute  = super.layoutAttributesForSupplementaryViewOfKind(elementKind, atIndexPath: indexPath)
        let contentOffset = collectionView?.contentOffset ?? CGPointZero
        var nextHeaderOrigin = CGPointZero
        var nextHeaderIndex : Int = 0

        if (indexPath.section + 1 < collectionView?.numberOfSections()) {
            nextHeaderIndex = indexPath.section + 1
            let nextIndexPath = NSIndexPath(forItem: 0, inSection: nextHeaderIndex)
            let nextHeaderFrame = super.layoutAttributesForSupplementaryViewOfKind(elementKind, atIndexPath: nextIndexPath)!.frame
            nextHeaderOrigin = nextHeaderFrame.origin
        }
        else {
            return layoutAttribute;
        }

        var headerFrame = layoutAttribute!.frame
        if (scrollDirection == UICollectionViewScrollDirection.Vertical) {
            let nextStickyCellY = nextHeaderOrigin.y - headerFrame.size.height
            let currentStickyCellY = max(contentOffset.y, headerFrame.origin.y)
            headerFrame.origin.y = min(currentStickyCellY, nextStickyCellY)
        }
        else {
            let nextStickyCellX = nextHeaderOrigin.x - headerFrame.size.width
            let currentStickyCellX = max(contentOffset.x, headerFrame.origin.x)
            headerFrame.origin.x = min(currentStickyCellX, nextStickyCellX)
        }

        layoutAttribute!.zIndex = stickyHeaderZIndex
        layoutAttribute!.frame = headerFrame
        return layoutAttribute
    }

图像:

enter image description here

0 个答案:

没有答案