【发布时间】:2018-07-16 11:28:04
【问题描述】:
我只是不知道如何在自定义单元格的 apply() 方法中访问自定义布局属性。
我必须为我的CollectionViewLayoutAtrributes 实现一个自定义布局属性,所以我对它们进行了子类化。到目前为止效果很好:
class TunedLayoutAttributes: UICollectionViewLayoutAttributes {
var customLayoutAttributeValue: CGFloat = 1000
override func copy(with zone: NSZone?) -> Any {
let copy = super.copy(with: zone) as! TunedLayoutAttributes
customLayoutAttributeValue = customLayoutAttributeValue
return copy
}
override func isEqual(_ object: Any?) -> Bool {
if let attributes = object as? TunedLayoutAttributes {
if attributes. customLayoutAttributeValue == customLayoutAttributeValue {
return super.isEqual (object)
}
}
return false
}
}
该值必须根据用户滚动交互动态变化。
现在我需要我的自定义单元格在来自自定义UICollectionViewLayout 类的invalidateLayout 调用之后更新它们的外观。据我所知,这通常也可以通过覆盖单元格类apply(_ layoutAttributes: UICollectionViewLayoutAttributes) 方法来完成。
通常是这样的:
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
let newFrame = // calculations here. CGRect(....)
layoutAttributes.frame = newFrame
}
现在缺少的化学反应:
与上面的apply() 示例不同,我的新customLayoutAttributeValue(当然?)不是方法中layoutAttributes: 的一部分。
所以我尝试将 layoutAttributes 向下转换为我的自定义类,如下所示:
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
let tunedLayoutAttributes = layoutAttributes as! TunedLayoutAttributes
tunedLayoutAttributes.customLayoutAttributeValue = // calculation here.
}
那么如何在 apply() 方法中访问 tuneLayoutAttributes? 任何帮助表示赞赏。感谢阅读!
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell uicollectionviewlayout downcast