【发布时间】:2018-04-24 03:16:57
【问题描述】:
我有一个自定义 UICollectionView 布局,可以在用户滚动时调整大小。随着标头在某一点收缩,它开始闪烁。
我猜问题是,当标题缩小时,集合视图认为它超出框架并可能将其出列,但随后它计算出它在框架中并重新排队,这可能是导致闪烁的原因。
class CustomLayout: UICollectionViewFlowLayout, UICollectionViewDelegateFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect) as! [UICollectionViewLayoutAttributes]
let offset = collectionView!.contentOffset ?? CGPoint.zero
let minY = -sectionInset.top
if (offset.y >= minY) {
let setOffset = fabs(170 - minY)
let extraOffset = fabs(offset.y - minY)
if offset.y <= 170 {
for attributes in layoutAttributes {
if let elementKind = attributes.representedElementKind {
if elementKind == UICollectionElementKindSectionHeader {
var frame = attributes.frame
frame.size.height = max(minY, headerReferenceSize.height - (extraOffset * 1.25))
frame.origin.y = frame.origin.y + (extraOffset * 1.25)
attributes.frame = frame
}
}
}
} else {
for attributes in layoutAttributes {
if let elementKind = attributes.representedElementKind {
if elementKind == UICollectionElementKindSectionHeader {
var frame = attributes.frame
frame.size.height = max(minY, headerReferenceSize.height - (setOffset * 1.25))
frame.origin.y = frame.origin.y + (setOffset * 1.25)
attributes.frame = frame
}
}
}
}
}
return layoutAttributes
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
这是一个显示行为的 gif。请注意它是如何开始正常并开始闪烁的。快速滚动也会产生不良影响。
有什么建议吗?
【问题讨论】:
-
你能录制一段视频来展示发生了什么吗?
-
@MaximoLucosi 感谢您的回复。我已经包含了一个描述行为的 gif。
-
这看起来是个好问题。能否提供一些github链接等供我们测试?
-
这种情况发生在模拟器和设备上吗?如果此工件当前仅在模拟器中观察到,模拟器是“实际大小”还是按比例缩小?我观察到模拟器在缩小时呈现异常,例如细线在超过亚像素阈值时闪烁可见/不可见。
-
有趣的问题
标签: ios swift uicollectionview