2017-04-24 5 views

答えて

1

私は答えを見つけましたが、実際はかなりシンプルでした。だから(UICollectionViewのように)、カスタムUICollectionViewLayoutで設定する必要があります。

let collectionView:IGListCollectionView = { 
    let layout = MyCustomLayout() 
    let view = IGListCollectionView(frame: .zero, collectionViewLayout: layout) 
    return view 
}() 

はここに私のカスタムレイアウトは次のようになります:共有のため

protocol MyCustomLayoutDelegate { 
    func heightForItem(inCollectionView: UICollectionView, at position: Int) -> CGFloat 
} 

class MyCustomLayout: UICollectionViewLayout, MyCustomLayoutDelegate { 

    var delegate:MyCustomLayoutDelegate? 

    private var cache = [UICollectionViewLayoutAttributes]() 

    private var aHeight:CGFloat = 0 

    private var contentWidth:CGFloat { 
     return collectionView!.bounds.width 
    } 

    override func prepare() { 

     if cache.isEmpty { 

      var yOffset:CGFloat = 0 

      for section in 0 ..< collectionView!.numberOfSections { 
       let contentHeight = (delegate ?? self).heightForItem(inCollectionView: collectionView!, at: section) 
       let leftAreaWidth = contentHeight - 1 
       let topAreaHeight:CGFloat = 20.0 
       let bottomAreaHeight = contentHeight - topAreaHeight - 1 
       let bottomAreaWidth = contentWidth - leftAreaWidth 

       let leftAreaIndexPath = IndexPath(item: 0, section: section) 
       let topAreaIndexPath = IndexPath(item: 1, section: section) 
       let bottomAreaIndexPath = IndexPath(item: 2, section: section) 

       let leftAreaFrame = CGRect(x: 0, y: yOffset, width: leftAreaWidth, height: leftAreaWidth) 
       let topAreaFrame = CGRect(x: leftAreaWidth, y: yOffset, width: bottomAreaWidth, height: topAreaHeight) 
       let bottomAreaFrame = CGRect(x: leftAreaWidth, y: yOffset + topAreaHeight, width: bottomAreaWidth, height: bottomAreaHeight) 

       let leftAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: leftAreaIndexPath) 
      leftAreaAttributes.frame = leftAreaFrame 
      cache.append(leftAreaAttributes) 

       let topAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: topAreaIndexPath) 
      topAreaAttributes.frame = topAreaFrame 
      cache.append(topAreaAttributes) 

       let bottomAreaAttributes = UICollectionViewLayoutAttributes(forCellWith: bottomAreaIndexPath) 
      bottomAreaAttributes.frame = bottomAreaFrame 
      cache.append(bottomAreaAttributes) 

       yOffset += contentHeight 
      } 

      aHeight = yOffset 
     } 
    } 

    override var collectionViewContentSize: CGSize { 
     return CGSize(width: contentWidth, height: aHeight) 
    } 

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

     return cache.filter { 
      $0.frame.intersects(rect) 
     } 
    } 

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { 

     return cache.first { 
      $0.indexPath == indexPath 
     } 

    } 

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { 

     if let oldWidth = collectionView?.bounds.width { 
      return oldWidth != newBounds.width 
     } 

     return false 
    } 

    override func invalidateLayout() { 
     super.invalidateLayout() 

     cache = [] 
     aHeight = 0 
    } 

    //Delegate 
    internal func heightForItem(inCollectionView: UICollectionView, at postion: Int) -> CGFloat { 
     return 0 
    } 
} 
+0

おかげyoninjaをだから私はこのようなものを持っています。私は同じ要望を持っていますが、あなたがカスタムレイアウトのために持っているものを詳細に分けてください。 – Amit89

+0

@ Amit89、私は答えを更新しました。 – yoninja

関連する問題