CPDFThumbnailViewController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // CPDFThumbnailViewController.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import ComPDFKit
  14. @objc public protocol CPDFThumbnailViewControllerDelegate: AnyObject {
  15. @objc optional func thumbnailViewController(_ thumbnailViewController: CPDFThumbnailViewController, pageIndex: Int)
  16. @objc optional func thumbnailViewControllerDismiss(_ thumbnailViewController: CPDFThumbnailViewController)
  17. }
  18. public class CPDFThumbnailViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
  19. public weak var pdfView: CPDFView?
  20. public weak var delegate: CPDFThumbnailViewControllerDelegate?
  21. public var collectionView: UICollectionView?
  22. // MARK: - Initializers
  23. public init(pdfView: CPDFView) {
  24. self.pdfView = pdfView
  25. super.init(nibName: nil, bundle: nil)
  26. }
  27. required init?(coder: NSCoder) {
  28. fatalError("init(coder:) has not been implemented")
  29. }
  30. // MARK: - UIViewController Methods
  31. public override func viewDidLoad() {
  32. super.viewDidLoad() // Do any additional setup after loading the view.
  33. changeLeftItem()
  34. let layout = UICollectionViewFlowLayout()
  35. layout.itemSize = CGSize(width: 110, height: 185)
  36. layout.sectionInset = UIEdgeInsets(top: 10, left: 5, bottom: 5, right: 5)
  37. layout.minimumInteritemSpacing = 5
  38. layout.minimumLineSpacing = 5
  39. collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  40. collectionView?.frame = view.bounds
  41. collectionView?.register(CPDFThumbnailViewCell.self, forCellWithReuseIdentifier: "thumnailCell")
  42. collectionView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  43. collectionView?.delegate = self
  44. collectionView?.dataSource = self
  45. collectionView?.alwaysBounceVertical = true
  46. if #available(iOS 11.0, *) {
  47. collectionView?.contentInsetAdjustmentBehavior = .always
  48. }
  49. view.backgroundColor = UIColor.white
  50. collectionView?.backgroundColor = UIColor(red: 0.804, green: 0.804, blue: 0.804, alpha: 1)
  51. guard let collectionView = self.collectionView else {
  52. return
  53. }
  54. view.addSubview(collectionView)
  55. updatePreferredContentSize(with: traitCollection)
  56. title = NSLocalizedString("Thumbnails", comment: "")
  57. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "CPDFEditClose", in: Bundle(for: CPDFThumbnailViewController.self), compatibleWith: nil), style: .done, target: self, action: #selector(buttonItemClicked_back(_:)))
  58. navigationItem.leftBarButtonItem = nil
  59. view.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  60. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  61. if self.pdfView?.document != nil {
  62. let indexPath = NSIndexPath(item: self.pdfView?.currentPageIndex ?? 0, section: 0)
  63. self.collectionView?.selectItem(at: indexPath as IndexPath, animated: false, scrollPosition: .centeredVertically)
  64. }
  65. }
  66. }
  67. public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  68. super.willTransition(to: newCollection, with: coordinator)
  69. updatePreferredContentSize(with: newCollection)
  70. }
  71. func updatePreferredContentSize(with traitCollection: UITraitCollection) {
  72. let width = UIScreen.main.bounds.size.width
  73. let height = UIScreen.main.bounds.size.height
  74. let mWidth = min(width, height)
  75. let mHeight = max(width, height)
  76. preferredContentSize = CGSize(width: view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.9 : mHeight * 0.9)
  77. }
  78. @objc func buttonItemClicked_back(_ button: UIButton) {
  79. if delegate?.thumbnailViewControllerDismiss?(self) != nil {
  80. delegate?.thumbnailViewControllerDismiss!(self)
  81. } else {
  82. dismiss(animated: true, completion: nil)
  83. }
  84. }
  85. // MARK: - Class Methods
  86. func setCollectViewSize(_ size: CGSize) {
  87. // Implementation goes here
  88. let layout = UICollectionViewFlowLayout()
  89. layout.itemSize = size
  90. layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
  91. layout.minimumInteritemSpacing = 5
  92. layout.minimumLineSpacing = 5
  93. self.collectionView?.collectionViewLayout = layout
  94. }
  95. // MARK: - UICollectionViewDataSource
  96. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  97. return Int(self.pdfView?.document.pageCount ?? 0)
  98. }
  99. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  100. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thumnailCell", for: indexPath) as? CPDFThumbnailViewCell
  101. let page = self.pdfView?.document.page(at: UInt(indexPath.item))
  102. let pageSize = self.pdfView?.document.pageSize(at: UInt(indexPath.item)) ?? CGSize.zero
  103. let multiple = max(pageSize.width / 110, pageSize.height / 173)
  104. cell?.imageSize = CGSize(width: pageSize.width / multiple, height: pageSize.height / multiple)
  105. cell?.setNeedsLayout()
  106. cell?.imageView?.image = page?.thumbnail(of: CGSize(width: pageSize.width / multiple, height: pageSize.height / multiple))
  107. cell?.textLabel?.text = "(indexPath.item + 1)"
  108. return cell ?? UICollectionViewCell()
  109. }
  110. // MARK: - UICollectionViewDelegate
  111. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  112. delegate?.thumbnailViewController?(self, pageIndex: indexPath.row)
  113. }
  114. }