2013-10-22 45 views
7

旋轉設備時,我看到UICollectionView與自定義佈局(UICollectionViewLayout的子類)的一些神祕行爲。UICollectionView界面旋轉與自定義佈局

我有一個簡單的水平滾動行單元格。當將設備從縱向旋轉到橫向時,其他單元格變得可見,以前不可見,並且這些出現單元格的動畫是錯誤的(它使用熟悉的重影效果,我認爲這是收集視圖的某種默認動畫效果佈局)。注意細胞出現在最左邊的這個動畫:

UICollectionViewRotationGhosting

有關自定義佈局設置一些細節:

  • 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看起來像一個巨大的黑盒子。

+0

http://stackoverflow.com/questions/12649271/what-is-the-best-best-way-to-disable-cross-fades- on-uicollectionview-uicollectio?rq = 1 –

+0

如果iOS檢測到轉換無法順利進行,即此場景,它將在與現有單元格相沖突的任何單元格上使用淡入淡出動畫。這在iOS7的UICollectionViews的WWDC視頻中有記錄。 – Tim

+0

@Arun_k這個問題與此不相關 - 他們要求澄清爲什麼'initial/finalLayout ...'在旋轉時被調用,事實上他們確認如上添加代碼__prevents__交叉淡入淡出動畫。這個問題關注在動畫邊界變化之後呈現的其他單元格。 – Stuart

回答

0

建立在the answer that Arun_k linked to上,看起來好像您可能需要返回與initialLayoutAttributesForAppearingItemAtIndexPath:不同的東西,除了nil以外的新單元格出現。 nil適用於已經在屏幕上的單元格,但可能不適合返回新單元格。也許你可以在這個方法中爲所有單元格返回一個固定值UICollectionViewLayoutAttributes來查看它是否改變了新插入的單元格的動畫行爲,然後從那裏繼續。

這個問題闡明瞭這個問題顯得更加輕薄:initialLayoutAttributesForAppearingItemAtIndexPath fired for all visible cells, not just inserted cells

+0

感謝您的回覆。不幸的是,正如我在我的問題中所說的,即使記錄返回的值顯示所有單元格的正確初始位置,返回非零值('[self layoutAttributesForItem ...]')也沒有任何區別,包括新出現的細胞。我會按照建議玩耍,看看我是否能找到更多。 – Stuart