2016-04-04 10 views
4

私は、セルがお互いの上に重なるカスタムUICollectionView + Layoutに取り組んでいます。さらに、アイテムの追加や削除のためのカスタムアニメーションを実装しています。オーバラップセルとカスタム挿入アニメーションと削除アニメーションを持つUICollectionView

項目の挿入/削除中に属性のプロパティーのうちzIndexのプロパティが無視されるようであることを除いて、すべて正常に機能しています。

適切な細胞:いくつかのセルを挿入した後 Proper cells

Cells with improper z order

はここでカスタム挿入/削除のアニメーションのための私の実装です。私の推測は、問題はこれらの方法のどこかにあることです。

override func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { 
    let attributes = layoutAttributesForItemAtIndexPath(itemIndexPath) 

    for updateItem in updateItems { 
     switch updateItem.updateAction { 
     case .Insert: 
      if updateItem.indexPathAfterUpdate == itemIndexPath { 
       let translation = collectionView!.bounds.height 
       attributes?.transform = CGAffineTransformMakeTranslation(0, translation) 
       break 
      } 
     default: 
      break 
     } 
    } 

    return attributes 
} 

override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { 
    for updateItem in updateItems { 
     switch updateItem.updateAction { 
     case .Delete: 
      if updateItem.indexPathBeforeUpdate == itemIndexPath { 
       let attributes = layoutAttributesForItemAtIndexPath(itemIndexPath) 
       let translation = collectionView!.bounds.height 
       attributes?.transform = CGAffineTransformMakeTranslation(0, translation) 
       return attributes 
      } 
     case .Move: 
      if updateItem.indexPathBeforeUpdate == itemIndexPath { 
       return layoutAttributesForItemAtIndexPath(updateItem.indexPathAfterUpdate!) 
      } 
     default: 
      break 
     } 
    } 
    let finalIndex = finalIndexForIndexPath(itemIndexPath) 
    let shiftedIndexPath = NSIndexPath(forItem: finalIndex, inSection: itemIndexPath.section) 

    return layoutAttributesForItemAtIndexPath(shiftedIndexPath) 
} 

private func finalIndexForIndexPath(indexPath: NSIndexPath) -> Int { 
    var newIndex = indexPath.item 
    for updateItem in updateItems { 
     switch updateItem.updateAction { 
     case .Insert: 
      if updateItem.indexPathAfterUpdate!.item <= newIndex { 
       newIndex += 1 
      } 
     case .Delete: 
      if updateItem.indexPathBeforeUpdate!.item < newIndex { 
       newIndex -= 1 
      } 
     case .Move: 
      if updateItem.indexPathBeforeUpdate!.item < newIndex { 
       newIndex -= 1 
      } 
      if updateItem.indexPathAfterUpdate!.item <= newIndex { 
       newIndex += 1 
      } 
     default: 
      break 
     } 
    } 
    return newIndex 
} 

物事は私が試してみた:

  • はそれがになってしまうだろう値にinitialLayoutAttributes...方法でzIndexを設定します。
  • アニメーションの後でコレクションビューを手動で並べ替える(これはかなりハッキーですが、これは避けることをお勧めします。
  • super.initialLayoutAttributes...などを呼び出して、それから返される属性を変更します。その問題は、新しいセルが挿入されたときに他のセルが移動するのを処理しないことです。代わりに、セルがフェードインまたはフェードアウトします。

プロジェクトの小さな再生者はmy GitHubにあります。それを複製して遊んでみてください。

答えて

3

私が思ったように、コレクションビューはアニメーション中にレイアウト属性を適切に適用していないようです。

正しいのZIndexに現れる挿入セルに上記のコードの結果を追加
override func applyLayoutAttributes(layoutAttributes: UICollectionViewLayoutAttributes) { 
    layer.zPosition = CGFloat(layoutAttributes.zIndex) 
} 

:幸いにも、UICollectionViewCell属性は、カスタムレイアウトを実装するための方法を提供します。

私はレーダーを記録し、リンクでコメントします。

+0

Hey Mark、私はカスタムオーバーラップコレクションビューセルのこの種の必要があります。このカスタムコレクションビューは、Cocoapodライブラリ形式でインストールして使用できますか? – Zhang

+2

うん、ここに私のレポがある:https://github.com/mpdifran/OverlapCollectionView – Mark

+0

私はスタンドアロンのCocoapodパッケージをもっと考えていた。ポッドファイルに追加してポッドインストールを実行し、抽出するのではなく独自のアプリで使い始めることができる関連するコードを既存のプロジェクトから外してください:DIは、他の人が恩恵を受けることができる非常に便利なライブラリになると考えています。 – Zhang

関連する問題