2014-04-21 29 views
1

我有一個帶有2個單元格的小集合視圖。這是垂直屏幕上的中心。當用戶選擇一個單元格時,一個新的單元格出現在集合視圖中,並且集合視圖相應地改變它的插入單元到屏幕中垂直中間的3個單元格。然而這種變化並不順利,它很跳躍。有沒有什麼方法來動畫collectionView插入更改?collectionView Insets中的動畫更改

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect { 
    NSArray* array = [super layoutAttributesForElementsInRect:rect]; 

    UICollectionViewLayoutAttributes* att = [array lastObject]; 
    if (att){ 
     CGFloat lastY = att.frame.origin.y + att.frame.size.height; 
     CGFloat diff = self.collectionView.frame.size.height - lastY; 
     if (diff > 0){ 
      UIEdgeInsets contentInsets = UIEdgeInsetsMake(diff/2, 0.0, 0.0, 0.0); 
      self.collectionView.contentInset = contentInsets; 
     } 
     self.diff = diff; 
    } 
    return array; 
} 

回答

3

這似乎並不可能進行動畫處理的CollectionView插圖,而無需創建定製UICollectionViewLayout所以我改變的邏輯和實現改變細胞的位置,而不是更改集合視圖的插圖。這給出了真正的流暢動畫!

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect { 
    NSArray* array = [super layoutAttributesForElementsInRect:rect]; 

    UICollectionViewLayoutAttributes* att = [array lastObject]; 
    if (att){ 
     CGFloat lastY = att.frame.origin.y + att.frame.size.height; 
     CGFloat diff = self.collectionView.frame.size.height - lastY; 
     if (diff > 0){ 
      for (UICollectionViewLayoutAttributes* a in array){ 
       a.frame = CGRectMake(a.frame.origin.x, a.frame.origin.y + diff/2, a.frame.size.width, a.frame.size.height) ; 
      } 
     } 
    } 
    return array; 
}