旋轉設備時,我看到UICollectionView
與自定義佈局(UICollectionViewLayout
的子類)的一些神祕行爲。UICollectionView界面旋轉與自定義佈局
我有一個簡單的水平滾動行單元格。當將設備從縱向旋轉到橫向時,其他單元格變得可見,以前不可見,並且這些出現單元格的動畫是錯誤的(它使用熟悉的重影效果,我認爲這是收集視圖的某種默認動畫效果佈局)。注意細胞出現在最左邊的這個動畫:
有關自定義佈局設置一些細節:
shouldInvalidateLayoutForBoundsChange:
回報YES
。- 在
layoutAttributesForItemAtIndexPath:
中,如果屬性尚未創建,則將屬性緩存在字典中。 - 在
layoutAttributesForElementsInRect:
我計算哪些細胞應該是可見的手,並且在每次返回它們的屬性之前微調它們的centre
屬性。
然後我有下面的代碼來處理初始/最終佈局屬性:
- (void)prepareForAnimatedBoundsChange:(CGRect)oldBounds
{
[super prepareForAnimatedBoundsChange:oldBounds];
self.animatingBoundsChange = YES;
}
- (void)finalizeAnimatedBoundsChange
{
[super finalizeAnimatedBoundsChange];
self.animatingBoundsChange = NO;
}
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
if (self.animatingBoundsChange) {
// If the view is rotating, appearing items should animate from their current attributes (specify `nil`).
// Both of these appear to do much the same thing:
//return [self layoutAttributesForItemAtIndexPath:itemIndexPath];
return nil;
}
return [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
if (self.animatingBoundsChange) {
// If the view is rotating, disappearing items should animate to their new attributes.
return [self layoutAttributesForItemAtIndexPath:itemIndexPath];
}
return [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
}
在我看來,對於新出現的電池的初始佈局屬性是有點不正確的(它畢竟在正確的位置結束)。但是,當我記錄從initalLayout...
方法返回的佈局屬性的center
屬性時,一切看起來都正確 - 均沿水平軸均勻分佈。
未正確動畫的單元格的唯一區別特徵是調用initialLayout...
方法時,它的單元格不會在[collectionView visibleCells]
數組中返回。然而,在它之前的layoutAttributesForElementsInRect:
確實需要識別它的佈局屬性。
那麼這是怎麼回事?一些對引擎蓋下發生的事情的深入瞭解將會非常有用...... UICollectionView
看起來像一個巨大的黑盒子。
http://stackoverflow.com/questions/12649271/what-is-the-best-best-way-to-disable-cross-fades- on-uicollectionview-uicollectio?rq = 1 –
如果iOS檢測到轉換無法順利進行,即此場景,它將在與現有單元格相沖突的任何單元格上使用淡入淡出動畫。這在iOS7的UICollectionViews的WWDC視頻中有記錄。 – Tim
@Arun_k這個問題與此不相關 - 他們要求澄清爲什麼'initial/finalLayout ...'在旋轉時被調用,事實上他們確認如上添加代碼__prevents__交叉淡入淡出動畫。這個問題關注在動畫邊界變化之後呈現的其他單元格。 – Stuart