gpt4 book ai didi

ios - 创建具有反向流布局的 UICollectionView

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:51 25 4
gpt4 key购买 nike

我想创建一个类似于聊天 View 的 View ,其中最新消息显示在屏幕底部。我希望将 UICollectionViewCells“固定”到屏幕底部——与默认流布局相反。我该怎么做?

我从另一个 stackoverflow 答案中得到了这段代码,但它不适用于可变高度项目(它需要):

import Foundation
import UIKit

class InvertedStackLayout: UICollectionViewLayout {
let cellHeight: CGFloat = 100 // Your cell height here...

override func prepare() {
super.prepare()
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttrs = [UICollectionViewLayoutAttributes]()

if let collectionView = self.collectionView {
for section in 0 ..< collectionView.numberOfSections {
if let numberOfSectionItems = numberOfItemsInSection(section) {
for item in 0 ..< numberOfSectionItems {
let indexPath = IndexPath(item: item, section: section)
let layoutAttr = layoutAttributesForItem(at: indexPath)

if let layoutAttr = layoutAttr, layoutAttr.frame.intersects(rect) {
layoutAttrs.append(layoutAttr)
}
}
}
}
}

return layoutAttrs
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let layoutAttr = UICollectionViewLayoutAttributes(forCellWith: indexPath)
let contentSize = self.collectionViewContentSize

layoutAttr.frame = CGRect(
x: 0, y: contentSize.height - CGFloat(indexPath.item + 1) * cellHeight,
width: contentSize.width, height: cellHeight)

return layoutAttr
}

func numberOfItemsInSection(_ section: Int) -> Int? {
if let collectionView = self.collectionView,
let numSectionItems = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: section)
{
return numSectionItems
}

return 0
}

override var collectionViewContentSize: CGSize {
get {
var height: CGFloat = 0
var bounds: CGRect = .zero

if let collectionView = self.collectionView {
for section in 0 ..< collectionView.numberOfSections {
if let numItems = numberOfItemsInSection(section) {
height += CGFloat(numItems) * cellHeight
}
}

bounds = collectionView.bounds
}

return CGSize(width: bounds.width, height: max(height, bounds.height))
}
}

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
if let oldBounds = self.collectionView?.bounds,
oldBounds.width != newBounds.width || oldBounds.height != newBounds.height
{
return true
}

return false
}
}

最佳答案

使用 CGAffineTransform 翻转您的 collectionView,使其从底部开始。

collectionView.transform = CGAffineTransform(scaleX: 1, y: -1)

现在唯一的问题是您所有的单元格都显示颠倒了。然后,您将变换应用到每个单元格以将其垂直翻转回来。

class InvertedCollectionViewFlowLayout: UICollectionViewFlowLayout {

// inverting the transform in the layout, rather than directly on the cell,
// is the only way I've found to prevent cells from flipping during animated
// cell updates

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attrs = super.layoutAttributesForItem(at: indexPath)
attrs?.transform = CGAffineTransform(scaleX: 1, y: -1)
return attrs
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attrsList = super.layoutAttributesForElements(in: rect)
if let list = attrsList {
for i in 0..<list.count {
list[i].transform = CGAffineTransform(scaleX: 1, y: -1)
}
}
return attrsList
}
}

关于ios - 创建具有反向流布局的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com