動(dòng)畫(huà)需要獲取當(dāng)前的 Cell ,下面的調(diào)用在 viewDidLoad 中,有時(shí)候返回 nil,有時(shí)候成功。
UICollectionView *cell = (UICollectionView*)[self.collectionView cellForItemAtIndexPath:indexPath];
簡(jiǎn)單的解決方法,是在調(diào)用之前添加
[self.collectionView layoutIfNeeded];
原因是,根據(jù)蘋(píng)果開(kāi)發(fā)者文檔:
UICollectionView如果 cell 不是 visible 的 或者 indexPath 超過(guò)有效范圍,就返回 nil。?
在 iOS 10 上 還有一種解法:
[self.collectionVew setPrefetching?Enabled: NO];
但這種就犧牲了性能。