我有UICollectionView
使用自定义UICollectionViewFlowLayout
的标头。 UICollectionView
包含照片,当滚动过某个点时,会显示一个带有活动指示符的页脚视图,直到加载了更多照片。如果用户快速滚动照片并到达页脚视图,则UICollectionView
标题的y原点变得不正确并且标题跳转到错误的位置,直到照片被加载。我想摆脱那种跳跃。这是自定义UICollectionViewFlowLayout
:
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
// Create a sticky UICollectionView Header
var answer: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElementsInRect(rect)! as [UICollectionViewLayoutAttributes]
let contentOffset = collectionView!.contentOffset
let missingSections = NSMutableIndexSet()
for layoutAttributes in answer {
if (layoutAttributes.representedElementCategory == .Cell) {
missingSections.addIndex(layoutAttributes.indexPath.section)
}
}
for layoutAttributes in answer {
if let representedElementKind = layoutAttributes.representedElementKind {
if representedElementKind == UICollectionElementKindSectionHeader {
missingSections.removeIndex(layoutAttributes.indexPath.section)
}
}
}
missingSections.enumerateIndexesUsingBlock { idx, stop in
let indexPath = NSIndexPath(forItem: 0, inSection: idx)
if let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) {
answer.append(layoutAttributes)
}
}
for layoutAttributes in answer {
if let representedElementKind = layoutAttributes.representedElementKind {
if representedElementKind == UICollectionElementKindSectionHeader {
let section = layoutAttributes.indexPath.section
let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section)
let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section)
let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section)
var cellExists: Bool = false
let (firstCellAttributes, lastCellAttributes): (UICollectionViewLayoutAttributes, UICollectionViewLayoutAttributes) = {
if (self.collectionView!.numberOfItemsInSection(section) > 0) {
cellExists = true
return (
self.layoutAttributesForItemAtIndexPath(firstCellIndexPath)!,
self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)!)
} else {
cellExists = false
return (
self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: firstCellIndexPath)!,
self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath: lastCellIndexPath)!)
}
}()
let topHeaderHeight = cellExists ? CGRectGetHeight(layoutAttributes.frame) : 0
let bottomHeaderHeight = CGRectGetHeight(layoutAttributes.frame)
let frameWithEdgeInsets = UIEdgeInsetsInsetRect(layoutAttributes.frame, collectionView!.contentInset)
var origin = frameWithEdgeInsets.origin
origin.y = min(max(contentOffset.y + collectionView!.contentInset.top,
(CGRectGetMinY(firstCellAttributes.frame) - topHeaderHeight - self.sectionInset.top))
,(CGRectGetMaxY(lastCellAttributes.frame) - bottomHeaderHeight + self.sectionInset.bottom))
layoutAttributes.zIndex = 1024
layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size)
}
}
}
return answer
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
}
是否有人知道可以进行哪些修改以确保在页脚视图可见时标题不会跳转?