AutoCat/AutoCat/Views/ImageGrid.swift
Selim Mustafaev 98bcfc540d ImageGrid
2020-12-21 19:13:47 +03:00

95 lines
3.4 KiB
Swift

import UIKit
import Kingfisher
class ImageCell: UICollectionViewCell {
private var imgView: UIImageView!
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.imgView = UIImageView()
self.contentView.addSubview(self.imgView)
self.imgView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.imgView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
self.imgView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
self.imgView.topAnchor.constraint(equalTo: self.contentView.topAnchor),
self.imgView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor)
])
}
func configure(with image: String) {
guard let url = URL(string: image) else { return }
self.imgView.kf.setImage(with: url)
}
}
class ImageGrid: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private var images: [String] = []
private var columnsCount: Int = 1
private var spacing: CGFloat = 0
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
self.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell")
self.dataSource = self
self.delegate = self
}
convenience init(columns: Int, spacing: CGFloat = 0) {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumInteritemSpacing = spacing
layout.minimumLineSpacing = spacing
layout.estimatedItemSize = .zero
self.init(frame: .zero, collectionViewLayout: layout)
self.columnsCount = columns
self.spacing = spacing
}
override var intrinsicContentSize: CGSize {
return self.collectionViewLayout.collectionViewContentSize
}
func set(images: [String]) {
self.images = images
self.reloadData()
self.invalidateIntrinsicContentSize()
}
func set(columnsCount: Int) {
self.columnsCount = columnsCount
self.reloadData()
self.invalidateIntrinsicContentSize()
}
// MARK: - UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = self.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as? ImageCell else { return UICollectionViewCell() }
cell.configure(with: self.images[indexPath.row])
return cell
}
// MARK: - UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellSize = (self.bounds.size.width - self.spacing*(CGFloat(self.columnsCount) - 1))/CGFloat(self.columnsCount)
print("====================================")
print("bounds: \(self.bounds)")
return CGSize(width: cellSize, height: cellSize)
}
}