NCViewerPDF.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. //
  2. // NCViewerPDF.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/02/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import PDFKit
  25. import SwiftUI
  26. class NCViewerPDF: UIViewController, NCViewerPDFSearchDelegate, UIGestureRecognizerDelegate {
  27. var metadata = tableMetadata()
  28. var imageIcon: UIImage?
  29. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. private var filePath = ""
  31. private var pdfView = PDFView()
  32. private var pdfThumbnailScrollView = UIScrollView()
  33. private var pdfThumbnailView = PDFThumbnailView()
  34. private var pdfDocument: PDFDocument?
  35. private let pageView = UIView()
  36. private let pageViewLabel = UILabel()
  37. private let thumbnailViewHeight: CGFloat = 70
  38. private let thumbnailViewWidth: CGFloat = 80
  39. private let thumbnailPadding: CGFloat = 2
  40. private let animateDuration: TimeInterval = 0.3
  41. private var defaultBackgroundColor: UIColor = .clear
  42. private var pdfThumbnailScrollViewleadingAnchor: NSLayoutConstraint?
  43. private var pdfThumbnailScrollViewWidthAnchor: NSLayoutConstraint?
  44. private var pdfThumbnailViewleadingAnchor: NSLayoutConstraint?
  45. private var pageViewLeftAnchor: NSLayoutConstraint?
  46. private var pageViewWidthAnchor: NSLayoutConstraint?
  47. // MARK: - View Life Cycle
  48. required init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. }
  51. override func viewDidLoad() {
  52. filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  53. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  54. let pageCount = CGFloat(pdfDocument?.pageCount ?? 0)
  55. defaultBackgroundColor = pdfView.backgroundColor
  56. view.backgroundColor = defaultBackgroundColor
  57. navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  58. navigationController?.navigationBar.prefersLargeTitles = false
  59. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  60. navigationItem.title = metadata.fileNameView
  61. // PDF VIEW
  62. pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
  63. pdfView.translatesAutoresizingMaskIntoConstraints = false
  64. pdfView.document = pdfDocument
  65. pdfView.autoScales = true
  66. pdfView.displayMode = .singlePageContinuous
  67. pdfView.displayDirection = .vertical
  68. pdfView.maxScaleFactor = 4.0
  69. pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
  70. view.addSubview(pdfView)
  71. NSLayoutConstraint.activate([
  72. pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  73. pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  74. pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
  75. ])
  76. if UIDevice.current.userInterfaceIdiom == .pad {
  77. pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: thumbnailViewWidth).isActive = true
  78. } else {
  79. pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
  80. }
  81. // PDF THUMBNAIL
  82. pdfThumbnailScrollView.translatesAutoresizingMaskIntoConstraints = false
  83. pdfThumbnailScrollView.backgroundColor = defaultBackgroundColor
  84. pdfThumbnailScrollView.showsVerticalScrollIndicator = false
  85. view.addSubview(pdfThumbnailScrollView)
  86. NSLayoutConstraint.activate([
  87. pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  88. pdfThumbnailScrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
  89. ])
  90. pdfThumbnailScrollViewleadingAnchor = pdfThumbnailScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
  91. pdfThumbnailScrollViewleadingAnchor?.isActive = true
  92. pdfThumbnailScrollViewWidthAnchor = pdfThumbnailScrollView.widthAnchor.constraint(equalToConstant: 0)
  93. pdfThumbnailScrollViewWidthAnchor?.isActive = true
  94. pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
  95. pdfThumbnailView.pdfView = pdfView
  96. pdfThumbnailView.layoutMode = .vertical
  97. pdfThumbnailView.thumbnailSize = CGSize(width: thumbnailViewHeight, height: thumbnailViewHeight)
  98. pdfThumbnailView.backgroundColor = .clear
  99. if UIDevice.current.userInterfaceIdiom == .pad {
  100. self.pdfThumbnailScrollView.isHidden = false
  101. } else {
  102. self.pdfThumbnailScrollView.isHidden = true
  103. }
  104. pdfThumbnailScrollView.addSubview(pdfThumbnailView)
  105. NSLayoutConstraint.activate([
  106. pdfThumbnailView.topAnchor.constraint(equalTo: pdfThumbnailScrollView.topAnchor),
  107. pdfThumbnailView.bottomAnchor.constraint(equalTo: pdfThumbnailScrollView.bottomAnchor),
  108. pdfThumbnailView.trailingAnchor.constraint(equalTo: pdfThumbnailScrollView.trailingAnchor),
  109. pdfThumbnailView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  110. ])
  111. pdfThumbnailViewleadingAnchor = pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.leadingAnchor)
  112. pdfThumbnailViewleadingAnchor?.isActive = true
  113. let contentViewCenterY = pdfThumbnailView.centerYAnchor.constraint(equalTo: pdfThumbnailScrollView.centerYAnchor)
  114. contentViewCenterY.priority = .defaultLow
  115. let contentViewHeight = pdfThumbnailView.heightAnchor.constraint(equalToConstant: CGFloat(pageCount * thumbnailViewHeight) + CGFloat(pageCount * thumbnailPadding) + 30)
  116. contentViewHeight.priority = .defaultLow
  117. NSLayoutConstraint.activate([
  118. contentViewCenterY,
  119. contentViewHeight
  120. ])
  121. // COUNTER PDF PAGE VIEW
  122. pageView.translatesAutoresizingMaskIntoConstraints = false
  123. pageView.layer.cornerRadius = 10
  124. pageView.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
  125. view.addSubview(pageView)
  126. NSLayoutConstraint.activate([
  127. pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
  128. pageView.heightAnchor.constraint(equalToConstant: 30)
  129. ])
  130. pageViewLeftAnchor = pageView.leftAnchor.constraint(equalTo: view.leftAnchor)
  131. pageViewLeftAnchor?.isActive = true
  132. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  133. pageViewWidthAnchor?.isActive = true
  134. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  135. pageViewLabel.textAlignment = .center
  136. pageViewLabel.textColor = .gray
  137. pageView.addSubview(pageViewLabel)
  138. NSLayoutConstraint.activate([
  139. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor),
  140. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor),
  141. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor),
  142. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor)
  143. ])
  144. // GESTURE
  145. let tapPdfView = UITapGestureRecognizer(target: self, action: #selector(tapPdfView))
  146. tapPdfView.numberOfTapsRequired = 1
  147. pdfView.addGestureRecognizer(tapPdfView)
  148. // recognize single / double tap
  149. for gesture in pdfView.gestureRecognizers! {
  150. tapPdfView.require(toFail: gesture)
  151. }
  152. let swipeLeftPdfView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  153. swipeLeftPdfView.direction = .left
  154. pdfView.addGestureRecognizer(swipeLeftPdfView)
  155. let swipeLeftpdfThumbnailScrollView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  156. swipeLeftpdfThumbnailScrollView.direction = .left
  157. pdfThumbnailScrollView.addGestureRecognizer(swipeLeftpdfThumbnailScrollView)
  158. let edgeLeftPdfView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  159. edgeLeftPdfView.edges = .left
  160. pdfView.addGestureRecognizer(edgeLeftPdfView)
  161. let edgeLeftView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  162. edgeLeftView.edges = .left
  163. view.addGestureRecognizer(edgeLeftView)
  164. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  165. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  166. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  167. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  168. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  169. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  170. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  171. NotificationCenter.default.addObserver(self, selector: #selector(goToPage), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  172. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
  173. setConstraints()
  174. handlePageChange()
  175. }
  176. @objc func viewUnload() {
  177. navigationController?.popViewController(animated: true)
  178. }
  179. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  180. super.viewWillTransition(to: size, with: coordinator)
  181. coordinator.animate(alongsideTransition: { context in
  182. if UIDevice.current.userInterfaceIdiom == .phone {
  183. self.pdfThumbnailScrollViewleadingAnchor?.constant = -self.thumbnailViewWidth
  184. self.pageViewLeftAnchor?.constant = 10
  185. self.pdfThumbnailScrollView.isHidden = true
  186. }
  187. }, completion: { context in
  188. self.setConstraints()
  189. })
  190. }
  191. deinit {
  192. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  193. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  194. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  195. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  196. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  197. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  198. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  199. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  200. NotificationCenter.default.removeObserver(self, name: Notification.Name.PDFViewPageChanged, object: nil)
  201. }
  202. // MARK: - NotificationCenter
  203. @objc func uploadedFile(_ notification: NSNotification) {
  204. if let userInfo = notification.userInfo as NSDictionary? {
  205. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  206. if errorCode == 0 && metadata.ocId == self.metadata.ocId {
  207. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  208. pdfView.document = pdfDocument
  209. pdfView.layoutDocumentView()
  210. }
  211. }
  212. }
  213. }
  214. @objc func favoriteFile(_ notification: NSNotification) {
  215. if let userInfo = notification.userInfo as NSDictionary? {
  216. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  217. if metadata.ocId == self.metadata.ocId {
  218. self.metadata = metadata
  219. }
  220. }
  221. }
  222. }
  223. @objc func moveFile(_ notification: NSNotification) {
  224. if let userInfo = notification.userInfo as NSDictionary? {
  225. if let ocId = userInfo["ocId"] as? String, let ocIdNew = userInfo["ocIdNew"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let metadataNew = NCManageDatabase.shared.getMetadataFromOcId(ocIdNew) {
  226. if metadata.ocId == self.metadata.ocId {
  227. self.metadata = metadataNew
  228. }
  229. }
  230. }
  231. }
  232. @objc func deleteFile(_ notification: NSNotification) {
  233. if let userInfo = notification.userInfo as NSDictionary? {
  234. if let ocId = userInfo["OcId"] as? String {
  235. if ocId == self.metadata.ocId {
  236. viewUnload()
  237. }
  238. }
  239. }
  240. }
  241. @objc func renameFile(_ notification: NSNotification) {
  242. if let userInfo = notification.userInfo as NSDictionary? {
  243. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  244. if metadata.ocId == self.metadata.ocId {
  245. self.metadata = metadata
  246. navigationItem.title = metadata.fileNameView
  247. }
  248. }
  249. }
  250. }
  251. @objc func searchText() {
  252. let viewerPDFSearch = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  253. viewerPDFSearch.delegate = self
  254. viewerPDFSearch.pdfDocument = pdfDocument
  255. let navigaionController = UINavigationController(rootViewController: viewerPDFSearch)
  256. self.present(navigaionController, animated: true)
  257. }
  258. @objc func goToPage() {
  259. guard let pdfDocument = pdfView.document else { return }
  260. let alertMessage = NSString(format: NSLocalizedString("_this_document_has_%@_pages_", comment: "") as NSString, "\(pdfDocument.pageCount)") as String
  261. let alertController = UIAlertController(title: NSLocalizedString("_go_to_page_", comment: ""), message: alertMessage, preferredStyle: .alert)
  262. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  263. alertController.addTextField(configurationHandler: { textField in
  264. textField.placeholder = NSLocalizedString("_page_", comment: "")
  265. textField.keyboardType = .decimalPad
  266. })
  267. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { [unowned self] _ in
  268. if let pageLabel = alertController.textFields?.first?.text {
  269. self.selectPage(with: pageLabel)
  270. }
  271. }))
  272. self.present(alertController, animated: true)
  273. }
  274. // MARK: - Action
  275. @objc func openMenuMore() {
  276. if imageIcon == nil { imageIcon = UIImage(named: "file_pdf") }
  277. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false, imageIcon: imageIcon)
  278. }
  279. // MARK: - Gesture Recognizer
  280. @objc func tapPdfView(_ recognizer: UITapGestureRecognizer) {
  281. if navigationController?.isNavigationBarHidden ?? false {
  282. navigationController?.setNavigationBarHidden(false, animated: true)
  283. } else {
  284. navigationController?.setNavigationBarHidden(true, animated: true)
  285. }
  286. }
  287. @objc func gestureOpenPdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  288. openPdfThumbnail()
  289. }
  290. @objc func gestureClosePdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  291. if recognizer.state == .recognized {
  292. closePdfThumbnail()
  293. }
  294. }
  295. // MARK: -
  296. func setConstraints() {
  297. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  298. UIView.animate(withDuration: animateDuration, animations: {
  299. if UIDevice.current.userInterfaceIdiom == .phone {
  300. self.pdfThumbnailScrollView.isHidden = true
  301. self.pdfThumbnailScrollViewleadingAnchor?.constant = -widthThumbnail
  302. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  303. self.pageViewLeftAnchor?.constant = 10
  304. } else {
  305. self.pdfThumbnailScrollViewleadingAnchor?.constant = 0
  306. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  307. self.pageViewLeftAnchor?.constant = widthThumbnail + 10
  308. }
  309. self.pdfThumbnailViewleadingAnchor?.constant = (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  310. self.view.layoutIfNeeded()
  311. self.pdfView.autoScales = true
  312. })
  313. }
  314. func openPdfThumbnail() {
  315. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  316. if UIDevice.current.userInterfaceIdiom == .phone && self.pdfThumbnailScrollView.isHidden {
  317. self.pdfThumbnailScrollView.isHidden = false
  318. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  319. UIView.animate(withDuration: animateDuration, animations: {
  320. self.pdfThumbnailScrollViewleadingAnchor?.constant = 0
  321. self.pageViewLeftAnchor?.constant = widthThumbnail + 10
  322. self.view.layoutIfNeeded()
  323. })
  324. }
  325. }
  326. func closePdfThumbnail() {
  327. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  328. if UIDevice.current.userInterfaceIdiom == .phone && !self.pdfThumbnailScrollView.isHidden {
  329. UIView.animate(withDuration: animateDuration) {
  330. self.pdfThumbnailScrollViewleadingAnchor?.constant = -widthThumbnail
  331. self.pageViewLeftAnchor?.constant = 10
  332. self.view.layoutIfNeeded()
  333. } completion: { _ in
  334. self.pdfThumbnailScrollView.isHidden = true
  335. }
  336. }
  337. }
  338. @objc func handlePageChange() {
  339. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  340. guard let totalPages = pdfView.document?.pageCount else { return }
  341. let visibleRect = CGRect(x: pdfThumbnailScrollView.contentOffset.x, y: pdfThumbnailScrollView.contentOffset.y, width: pdfThumbnailScrollView.bounds.size.width, height: pdfThumbnailScrollView.bounds.size.height)
  342. let centerPoint = CGPoint(x: visibleRect.size.width/2, y: visibleRect.size.height/2)
  343. let currentPageY = CGFloat(curPage) * thumbnailViewHeight + CGFloat(curPage) * thumbnailPadding
  344. var gotoY = currentPageY - centerPoint.y
  345. let startY = visibleRect.origin.y < 0 ? 0 : (visibleRect.origin.y + thumbnailViewHeight)
  346. let endY = visibleRect.origin.y + visibleRect.height + thumbnailViewHeight
  347. if currentPageY < startY {
  348. if gotoY < 0 { gotoY = 0 }
  349. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  350. } else if currentPageY > endY {
  351. if gotoY > pdfThumbnailView.frame.height - visibleRect.height {
  352. gotoY = pdfThumbnailView.frame.height - visibleRect.height
  353. }
  354. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  355. } else {
  356. print("visible")
  357. }
  358. pageView.alpha = 1
  359. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  360. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  361. UIView.animate(withDuration: 1.0, delay: 2.5, animations: {
  362. self.pageView.alpha = 0
  363. })
  364. }
  365. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  366. pdfSelection.color = .yellow
  367. pdfView.currentSelection = pdfSelection
  368. pdfView.go(to: pdfSelection)
  369. }
  370. private func selectPage(with label: String) {
  371. guard let pdf = pdfView.document else { return }
  372. if let pageNr = Int(label) {
  373. if pageNr > 0 && pageNr <= pdf.pageCount {
  374. if let page = pdf.page(at: pageNr - 1) {
  375. self.pdfView.go(to: page)
  376. }
  377. } else {
  378. let alertController = UIAlertController(title: NSLocalizedString("_invalid_page_", comment: ""),
  379. message: NSLocalizedString("_the_entered_page_number_doesn't_exist_", comment: ""),
  380. preferredStyle: .alert)
  381. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: nil))
  382. self.present(alertController, animated: true, completion: nil)
  383. }
  384. }
  385. }
  386. }