UICollectionView自定义Flow Layout,单元格在横向中消失

时间:2016-12-02 11:22:39

标签: ios xcode uicollectionview uicollectionviewlayout

我制作了一个自定义的流程布局,它在纵向上完美运行,但是一旦我去景观,左边的单元格就会在滚动时消失。

启用了分页,如果我向后滚动1 px,则会再次出现该单元格。

Gif of the layout

我的布局属性覆盖

我试图使用变换而不是改变帧,结果是相同的。

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var att = super.layoutAttributesForElements(in: rect)!

    if !(delegate?.reelPlayerFlowIsPreviewMode() ?? true) {
        return att
    }
    let region = CGRect(x: (self.collectionView?.contentOffset.x)!,
                        y: (self.collectionView?.contentOffset.y)!,
                        width: (self.collectionView?.bounds.size.width)!,
                        height: (self.collectionView?.bounds.size.height)!)

    let center = CGPoint(x: region.midX, y: region.midY)

    for theCell in att {

        print("\(theCell.indexPath.item)\n\(theCell)\n")
        let cell = theCell.copy() as! UICollectionViewLayoutAttributes
        var f = cell.frame
        let cellCenter = CGPoint(x: f.midX, y: f.midY)
        let realDistance = min(center.x - cellCenter.x, region.width)
        let distance = abs(realDistance)
        let d = (region.width - distance) / region.width
        let p = (max(d, ReelPlayerFlowLayout.minPercent) * ReelPlayerFlowLayout.maxPercent)

        f.origin.x += (realDistance * ((1 - ReelPlayerFlowLayout.maxPercent) + (ReelPlayerFlowLayout.maxPercent - ReelPlayerFlowLayout.minPercent)))

        cell.frame = f
        cell.size = CGSize (width: f.width * p, height: f.height * p)            
        let index = att.index(of: theCell)!
        att[index] = cell
    }

    return att
}

0 个答案:

没有答案