Advertisement
Larme

Untitled

Jun 13th, 2020
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.36 KB | None | 0 0
  1. class ViewController: UIViewController {
  2.     @IBOutlet var collectionView: UICollectionView!
  3.    
  4.     var colors: [UIColor] = [.red, .blue, .purple, .orange, .cyan, .green, .magenta, .lightGray, .blue, .green, .systemPink]
  5.    
  6.     override func viewDidLoad() {
  7.         super.viewDidLoad()
  8.         let layout = CustomLayout(unitSize: CGSize(width: 106, height: 88))
  9.         collectionView.setCollectionViewLayout(layout, animated: true)
  10.     }
  11.    
  12.     override func viewDidLayoutSubviews() {
  13.         super.viewDidLayoutSubviews()
  14.     }
  15. }
  16.  
  17. extension ViewController: UICollectionViewDataSource {
  18.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  19.         return 10
  20.     }
  21.    
  22.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  23.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CustomCell
  24.        
  25.         cell.label.text = String(indexPath.row)
  26.         cell.backgroundColor = colors[indexPath.row % colors.count]
  27.         return cell
  28.     }
  29. }
  30.  
  31.  
  32.  
  33. class CustomLayout: UICollectionViewLayout {
  34.  
  35.     private var cellLayouts: [IndexPath: UICollectionViewLayoutAttributes] = [:]
  36.     private var smallUnitSize: CGSize = .zero
  37.  
  38.     convenience init(unitSize: CGSize) {
  39.         self.init()
  40.         self.smallUnitSize = unitSize
  41.     }
  42.    
  43.     override func prepare() {
  44.         cellLayouts.removeAll()
  45.         guard let numberOfRows = collectionView?.numberOfItems(inSection: 0) else { return }
  46.         for aRow in 0..<numberOfRows {
  47.             let indexPath = IndexPath(item: aRow, section: 0)
  48.             let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
  49.            
  50.             let frame: CGRect
  51.             switch indexPath.row {
  52.                 case 0:
  53.                     frame = CGRect(x: 0, y: 0,
  54.                                    width: smallUnitSize.width * 2, height: smallUnitSize.height * 2)
  55.                 case 1:
  56.                     frame = CGRect(origin: CGPoint(x: 0, y: smallUnitSize.height * 2),
  57.                                    size: smallUnitSize)
  58.                 case 2:
  59.                     frame = CGRect(origin: CGPoint(x: smallUnitSize.width, y: smallUnitSize.height * 2),
  60.                                    size: smallUnitSize)
  61.                 case 3:
  62.                     frame = CGRect(origin: CGPoint(x: smallUnitSize.width * 2, y: 0),
  63.                                    size: smallUnitSize)
  64.                 case 4:
  65.                     frame = CGRect(origin: CGPoint(x: smallUnitSize.width * 2, y: smallUnitSize.height),
  66.                                    size: smallUnitSize)
  67.                 case 5:
  68.                     frame = CGRect(origin: CGPoint(x: smallUnitSize.width * 2, y: smallUnitSize.height * 2),
  69.                                    size: smallUnitSize)
  70.                 default:
  71.                     let rest = indexPath.row - 5
  72.                     frame = CGRect(x: smallUnitSize.width * 3 * CGFloat(rest), y: 0,
  73.                                    width: smallUnitSize.width * 3, height: smallUnitSize.height * 3)
  74.             }
  75.             attributes.frame = frame
  76.             cellLayouts[indexPath] = attributes
  77.         }
  78. //        cellLayouts.sorted(by: { $0.key < $1.key }).forEach({ print("\($0.key): \($0.value.frame)") })
  79.     }
  80.    
  81.     override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  82.         return cellLayouts.compactMap { $0.value.frame.intersects(rect) ? $0.value : nil }
  83.     }
  84.    
  85.     override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
  86.         return cellLayouts[indexPath]
  87.     }
  88.    
  89.     override var collectionViewContentSize: CGSize {
  90.         guard let numberOfRows = collectionView?.numberOfItems(inSection: 0) else { return .zero }
  91.         switch numberOfRows {
  92.             case 0...5:
  93.                 return CGSize(width: smallUnitSize.width * 3, height: smallUnitSize.height * 3)
  94.             default:
  95.                 let rest = numberOfRows - 6
  96.                 return CGSize(width: (smallUnitSize.width * 3) * CGFloat((1 + rest)), height: smallUnitSize.height * 3)
  97.         }
  98.     }
  99.    
  100.     override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
  101.         return true
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement