NCViewerImageView.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import UIKit
  2. public class NCViewerImageView: NCViewerImageNibLoadingView {
  3. @IBOutlet weak var collectionView: UICollectionView!
  4. public var assets: [NCViewerImageAsset?]? {
  5. didSet {
  6. self.collectionView.reloadData()
  7. }
  8. }
  9. private var preselectedIndex: Int = 0
  10. override public func willMove(toSuperview newSuperview: UIView?) {
  11. super.willMove(toSuperview: newSuperview)
  12. self.collectionView.register(UINib.init(nibName: String(describing: NCViewerImageCollectionViewCell.self), bundle: Bundle(for: type(of: self))), forCellWithReuseIdentifier: NCViewerImageCollectionViewCell.reusableIdentifier)
  13. }
  14. public override func layoutSubviews() {
  15. super.layoutSubviews()
  16. self.collectionView.collectionViewLayout.invalidateLayout()
  17. if let indexPath = self.collectionView.indexPathsForVisibleItems.last {
  18. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  19. self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
  20. }
  21. }
  22. }
  23. public func preselectItem(at index: Int) {
  24. self.preselectedIndex = index
  25. }
  26. public override func draw(_ rect: CGRect) {
  27. super.draw(rect)
  28. self.collectionView.scrollToItem(at: IndexPath(row: self.preselectedIndex, section: 0), at: .centeredHorizontally, animated: false)
  29. }
  30. }
  31. extension NCViewerImageView: UICollectionViewDataSource {
  32. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  33. return assets?.count ?? 0
  34. }
  35. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  36. let cell: NCViewerImageCollectionViewCell = (collectionView.dequeueReusableCell(withReuseIdentifier: NCViewerImageCollectionViewCell.reusableIdentifier, for: indexPath) as? NCViewerImageCollectionViewCell)!
  37. cell.withImageAsset(assets?[indexPath.row])
  38. cell.delegate = self
  39. return cell
  40. }
  41. }
  42. extension NCViewerImageView: UICollectionViewDelegate {
  43. public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  44. (cell as? NCViewerImageCollectionViewCell)?.cancelPendingDataTask()
  45. }
  46. public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  47. (cell as? NCViewerImageCollectionViewCell)?.withImageAsset(assets?[indexPath.row])
  48. }
  49. }
  50. extension NCViewerImageView: UICollectionViewDelegateFlowLayout {
  51. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  52. return CGSize(width: floor(collectionView.frame.size.width), height: floor(collectionView.frame.size.height))
  53. }
  54. }
  55. extension NCViewerImageView: NCViewerImageCollectionViewCellDelegate {
  56. func didStartZooming(_ cell: NCViewerImageCollectionViewCell) {
  57. }
  58. }