swift 使用UICollectionViewFlowLayout创建水平UICollectionView,能够同时向左和向右对齐单元格

jw5wzhpr  于 2023-06-28  发布在  Swift
关注(0)|答案(1)|浏览(138)

我试图创建一个水平的UICollectionView与可能性对齐单元格的左侧和其他部分的权利。左右单元格之间应该是空白。是否可以使用UICollectionView和UICollectionViewFlowLayout?
我尝试了这个版本的UICollectionViewFlowLayout,但它不能正常工作。我正在尝试了解如何动态计算偏移量。

class LeftRightAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
    var offset: CGFloat = 30
    
    fileprivate var layoutAttributes = [UICollectionViewLayoutAttributes]()
    fileprivate var contentSize = CGSize.zero
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard let superAttributes = super.layoutAttributesForElements(in: rect) else { return nil }
        var attributes = superAttributes
        let centerX = collectionView!.contentOffset.x + collectionView!.bounds.width / 2.0
        
        for attribute in attributes {
            let distance = abs(attribute.center.x - centerX)
            let offset = min(abs(distance), self.offset)
            let factor = (self.offset - offset) / self.offset
            let translation = factor * attribute.size.width / 2
            attribute.center.x += distance < self.offset ? (attribute.center.x < centerX ? -translation : translation) : 0
        }
        return attributes
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
}
8zzbczxx

8zzbczxx1#

请在layoutAttributesForElements函数中尝试以下代码:

guard let superAttributes = super.layoutAttributesForElements(in: rect) else { return nil }
var attributes = superAttributes
let centerX = collectionView!.contentOffset.x + collectionView!.bounds.width / 2.0

// Separate the attributes for left and right cells
var leftAttributes: [UICollectionViewLayoutAttributes] = []
var rightAttributes: [UICollectionViewLayoutAttributes] = []
for attribute in attributes {
    if attribute.center.x < centerX {
        leftAttributes.append(attribute)
    } else {
        rightAttributes.append(attribute)
    }
}

// Calculate the total width of the left and right cells
let leftWidth = leftAttributes.reduce(0) { $0 + $1.size.width }
let rightWidth = rightAttributes.reduce(0) { $0 + $1.size.width }

// Adjust the position of the left cells
var xPosition: CGFloat = 0
for attribute in leftAttributes {
    let frame = attribute.frame
    attribute.frame = CGRect(x: xPosition, y: frame.origin.y, width: frame.size.width, height: frame.size.height)
    xPosition += frame.size.width
}

// Calculate the offset for the right cells and adjust their position
let offset = (collectionViewContentSize.width - leftWidth - rightWidth) / 2
xPosition = collectionViewContentSize.width - rightWidth
for attribute in rightAttributes {
    let frame = attribute.frame
    attribute.frame = CGRect(x: xPosition + offset, y: frame.origin.y, width: frame.size.width, height: frame.size.height)
    xPosition += frame.size.width
}

return attributes

相关问题