我正在try 创建一个水平的UICollectionView,可以将部分单元格左对齐,另一部分右对齐.左右单元格之间应留有空格.是否可以使用UICollectionView和UICollectionViewFlowLayout?

我try 了这个版本的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
    }
}

推荐答案

请在layoutAttributesForElements函数中try 此代码:

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

Swift相关问答推荐

多个提取来计算核心数据中的记录

如何让CTFontCreateUIFontForLanguage与汉字和表情包协同工作?

按变换zoom 在UIView中不起作用

OBJC代码中Swift 演员的伊瓦尔:原子还是非原子?

从后台线程获取@发布属性的值(不更改它)是正确的吗?

声明一个协议时使用和不使用任何单词有什么区别?

更改 SwiftUI 中按钮矩阵的填充 colored颜色

如何使用Swift宏和@Observable和@Environment?

使用View()和List()无法显示影子

使用 RxSwift 围绕 async/await 方法创建 Observable

关于变量的视图的 SwiftUI 生命周期

在 SwiftUI 中操作绑定变量

如何在 Swift 中的两个 UIView 中心之间添加线?

如何从 LLDB 调用带有断点的 Swift 函数?

Swift - 获取本地日期和时间

我可以在 Swift 中为泛型类型 T 分配默认类型吗?

如何从一个可观察的数组创建一个可观察的数组?

Alamofire:如何全局处理错误

如何在 Swift 中重写 setter

从 Swift 初始化程序调用方法