需要初始化的,應(yīng)該寫(xiě)在初始化方法init()里
網(wǎng)上很多說(shuō)設(shè)置要寫(xiě)在prepare()里的,都是錯(cuò)誤的說(shuō)法
override init() {
super.init()
}
prepare()會(huì)重復(fù)調(diào)用,需要設(shè)置collectionView或者其他init時(shí)無(wú)法獲取的數(shù)據(jù)才應(yīng)該用prepare(),避免重復(fù)設(shè)置
override func prepare() {
super.prepare()
collectionView?.decelerationRate = .fast
}
//是否變化時(shí)重新布局
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
//視圖變化的數(shù)據(jù)
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let array = super.layoutAttributesForElements(in: rect)
//這是要顯示可見(jiàn)cell,不是所有的,如果要顯示所有的,需要
//self.layoutAttributesForItem(at: indexPath) 便利獲取
// array 里面元素是一個(gè)cell的所有相關(guān)數(shù)據(jù)UICollectionViewLayoutAttributes
//更改 元素的frame等來(lái)達(dá)到修改自定義cell布局的效果
//如果要特殊處理某個(gè)單獨(dú)元素,layoutAttributesForItem(at indexPath: IndexPath) 然后在array中返回(不要重復(fù)添加已有的)
return array
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
//手動(dòng)滾動(dòng)時(shí)候的偏移數(shù)據(jù)(抬手)
//可以設(shè)置滾動(dòng)到某處時(shí)的處理,比如分頁(yè)
return proposedContentOffset
}