NCViewerImageView.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import UIKit
  2. 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 = -1
  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.01) {
  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. if preselectedIndex != -1 {
  29. self.collectionView.scrollToItem(at: IndexPath(row: self.preselectedIndex, section: 0), at: .centeredHorizontally, animated: false)
  30. preselectedIndex = -1
  31. }
  32. }
  33. }
  34. extension NCViewerImageView: UICollectionViewDataSource {
  35. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  36. return assets?.count ?? 0
  37. }
  38. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  39. let cell: NCViewerImageCollectionViewCell = (collectionView.dequeueReusableCell(withReuseIdentifier: NCViewerImageCollectionViewCell.reusableIdentifier, for: indexPath) as? NCViewerImageCollectionViewCell)!
  40. cell.withImageAsset(assets?[indexPath.row])
  41. cell.delegate = self
  42. return cell
  43. }
  44. }
  45. extension NCViewerImageView: UICollectionViewDelegate {
  46. public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  47. (cell as? NCViewerImageCollectionViewCell)?.cancelPendingDataTask()
  48. }
  49. public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  50. //(cell as? NCViewerImageCollectionViewCell)?.withImageAsset(assets?[indexPath.row])
  51. }
  52. }
  53. extension NCViewerImageView: UICollectionViewDelegateFlowLayout {
  54. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  55. return CGSize(width: floor(collectionView.frame.size.width), height: floor(collectionView.frame.size.height))
  56. }
  57. }
  58. extension NCViewerImageView: NCViewerImageCollectionViewCellDelegate {
  59. func didStartZooming(_ cell: NCViewerImageCollectionViewCell) {
  60. }
  61. }