NCViewerPDF.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.displayMode = .singlePageContinuous
  66. pdfView.displayDirection = .vertical
  67. pdfView.maxScaleFactor = 4.0
  68. pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
  69. view.addSubview(pdfView)
  70. NSLayoutConstraint.activate([
  71. pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  72. pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  73. pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
  74. ])
  75. if UIDevice.current.userInterfaceIdiom == .pad {
  76. pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: thumbnailViewWidth).isActive = true
  77. } else {
  78. pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
  79. }
  80. // PDF THUMBNAIL
  81. pdfThumbnailScrollView.translatesAutoresizingMaskIntoConstraints = false
  82. pdfThumbnailScrollView.backgroundColor = defaultBackgroundColor
  83. pdfThumbnailScrollView.showsVerticalScrollIndicator = false
  84. view.addSubview(pdfThumbnailScrollView)
  85. NSLayoutConstraint.activate([
  86. pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  87. pdfThumbnailScrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
  88. ])
  89. pdfThumbnailScrollViewleadingAnchor = pdfThumbnailScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
  90. pdfThumbnailScrollViewleadingAnchor?.isActive = true
  91. pdfThumbnailScrollViewWidthAnchor = pdfThumbnailScrollView.widthAnchor.constraint(equalToConstant: 0)
  92. pdfThumbnailScrollViewWidthAnchor?.isActive = true
  93. pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
  94. pdfThumbnailView.pdfView = pdfView
  95. pdfThumbnailView.layoutMode = .vertical
  96. pdfThumbnailView.thumbnailSize = CGSize(width: thumbnailViewHeight, height: thumbnailViewHeight)
  97. pdfThumbnailView.backgroundColor = .clear
  98. if UIDevice.current.userInterfaceIdiom == .pad {
  99. self.pdfThumbnailScrollView.isHidden = false
  100. } else {
  101. self.pdfThumbnailScrollView.isHidden = true
  102. }
  103. pdfThumbnailScrollView.addSubview(pdfThumbnailView)
  104. NSLayoutConstraint.activate([
  105. pdfThumbnailView.topAnchor.constraint(equalTo: pdfThumbnailScrollView.topAnchor),
  106. pdfThumbnailView.bottomAnchor.constraint(equalTo: pdfThumbnailScrollView.bottomAnchor),
  107. pdfThumbnailView.trailingAnchor.constraint(equalTo: pdfThumbnailScrollView.trailingAnchor),
  108. pdfThumbnailView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  109. ])
  110. pdfThumbnailViewleadingAnchor = pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.leadingAnchor)
  111. pdfThumbnailViewleadingAnchor?.isActive = true
  112. let contentViewCenterY = pdfThumbnailView.centerYAnchor.constraint(equalTo: pdfThumbnailScrollView.centerYAnchor)
  113. contentViewCenterY.priority = .defaultLow
  114. let contentViewHeight = pdfThumbnailView.heightAnchor.constraint(equalToConstant: CGFloat(pageCount * thumbnailViewHeight) + CGFloat(pageCount * thumbnailPadding) + 30)
  115. contentViewHeight.priority = .defaultLow
  116. NSLayoutConstraint.activate([
  117. contentViewCenterY,
  118. contentViewHeight
  119. ])
  120. // COUNTER PDF PAGE VIEW
  121. pageView.translatesAutoresizingMaskIntoConstraints = false
  122. pageView.layer.cornerRadius = 10
  123. pageView.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
  124. view.addSubview(pageView)
  125. NSLayoutConstraint.activate([
  126. pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
  127. pageView.heightAnchor.constraint(equalToConstant: 30)
  128. ])
  129. pageViewLeftAnchor = pageView.leftAnchor.constraint(equalTo: view.leftAnchor)
  130. pageViewLeftAnchor?.isActive = true
  131. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  132. pageViewWidthAnchor?.isActive = true
  133. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  134. pageViewLabel.textAlignment = .center
  135. pageViewLabel.textColor = .gray
  136. pageView.addSubview(pageViewLabel)
  137. NSLayoutConstraint.activate([
  138. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor),
  139. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor),
  140. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor),
  141. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor)
  142. ])
  143. // GESTURE
  144. let tapPdfView = UITapGestureRecognizer(target: self, action: #selector(tapPdfView))
  145. tapPdfView.numberOfTapsRequired = 1
  146. pdfView.addGestureRecognizer(tapPdfView)
  147. // recognize single / double tap
  148. for gesture in pdfView.gestureRecognizers! {
  149. tapPdfView.require(toFail: gesture)
  150. }
  151. let swipeLeftPdfView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  152. swipeLeftPdfView.direction = .left
  153. pdfView.addGestureRecognizer(swipeLeftPdfView)
  154. let swipeLeftpdfThumbnailScrollView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  155. swipeLeftpdfThumbnailScrollView.direction = .left
  156. pdfThumbnailScrollView.addGestureRecognizer(swipeLeftpdfThumbnailScrollView)
  157. let edgeLeftPdfView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  158. edgeLeftPdfView.edges = .left
  159. pdfView.addGestureRecognizer(edgeLeftPdfView)
  160. let edgeLeftView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  161. edgeLeftView.edges = .left
  162. view.addGestureRecognizer(edgeLeftView)
  163. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  164. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  165. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  166. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  167. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  168. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  169. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  170. NotificationCenter.default.addObserver(self, selector: #selector(goToPage), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  171. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
  172. setConstraints()
  173. handlePageChange()
  174. }
  175. @objc func viewUnload() {
  176. navigationController?.popViewController(animated: true)
  177. }
  178. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  179. super.viewWillTransition(to: size, with: coordinator)
  180. coordinator.animate(alongsideTransition: { context in
  181. if UIDevice.current.userInterfaceIdiom == .phone {
  182. self.pdfThumbnailScrollViewleadingAnchor?.constant = -self.thumbnailViewWidth
  183. self.pageViewLeftAnchor?.constant = 10
  184. self.pdfThumbnailScrollView.isHidden = true
  185. }
  186. }, completion: { context in
  187. self.setConstraints()
  188. })
  189. }
  190. deinit {
  191. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  192. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  193. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  194. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  195. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  196. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  197. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  198. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  199. NotificationCenter.default.removeObserver(self, name: Notification.Name.PDFViewPageChanged, object: nil)
  200. }
  201. // MARK: - NotificationCenter
  202. @objc func uploadedFile(_ notification: NSNotification) {
  203. if let userInfo = notification.userInfo as NSDictionary? {
  204. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  205. if errorCode == 0 && metadata.ocId == self.metadata.ocId {
  206. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  207. pdfView.document = pdfDocument
  208. pdfView.layoutDocumentView()
  209. }
  210. }
  211. }
  212. }
  213. @objc func favoriteFile(_ notification: NSNotification) {
  214. if let userInfo = notification.userInfo as NSDictionary? {
  215. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  216. if metadata.ocId == self.metadata.ocId {
  217. self.metadata = metadata
  218. }
  219. }
  220. }
  221. }
  222. @objc func moveFile(_ notification: NSNotification) {
  223. if let userInfo = notification.userInfo as NSDictionary? {
  224. 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) {
  225. if metadata.ocId == self.metadata.ocId {
  226. self.metadata = metadataNew
  227. }
  228. }
  229. }
  230. }
  231. @objc func deleteFile(_ notification: NSNotification) {
  232. if let userInfo = notification.userInfo as NSDictionary? {
  233. if let ocId = userInfo["OcId"] as? String {
  234. if ocId == self.metadata.ocId {
  235. viewUnload()
  236. }
  237. }
  238. }
  239. }
  240. @objc func renameFile(_ notification: NSNotification) {
  241. if let userInfo = notification.userInfo as NSDictionary? {
  242. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  243. if metadata.ocId == self.metadata.ocId {
  244. self.metadata = metadata
  245. navigationItem.title = metadata.fileNameView
  246. }
  247. }
  248. }
  249. }
  250. @objc func searchText() {
  251. let viewerPDFSearch = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  252. viewerPDFSearch.delegate = self
  253. viewerPDFSearch.pdfDocument = pdfDocument
  254. let navigaionController = UINavigationController(rootViewController: viewerPDFSearch)
  255. self.present(navigaionController, animated: true)
  256. }
  257. @objc func goToPage() {
  258. guard let pdfDocument = pdfView.document else { return }
  259. let alertMessage = NSString(format: NSLocalizedString("_this_document_has_%@_pages_", comment: "") as NSString, "\(pdfDocument.pageCount)") as String
  260. let alertController = UIAlertController(title: NSLocalizedString("_go_to_page_", comment: ""), message: alertMessage, preferredStyle: .alert)
  261. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  262. alertController.addTextField(configurationHandler: { textField in
  263. textField.placeholder = NSLocalizedString("_page_", comment: "")
  264. textField.keyboardType = .decimalPad
  265. })
  266. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { [unowned self] _ in
  267. if let pageLabel = alertController.textFields?.first?.text {
  268. self.selectPage(with: pageLabel)
  269. }
  270. }))
  271. self.present(alertController, animated: true)
  272. }
  273. // MARK: - Action
  274. @objc func openMenuMore() {
  275. if imageIcon == nil { imageIcon = UIImage(named: "file_pdf") }
  276. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false, imageIcon: imageIcon)
  277. }
  278. // MARK: - Gesture Recognizer
  279. @objc func tapPdfView(_ recognizer: UITapGestureRecognizer) {
  280. if navigationController?.isNavigationBarHidden ?? false {
  281. navigationController?.setNavigationBarHidden(false, animated: true)
  282. } else {
  283. navigationController?.setNavigationBarHidden(true, animated: true)
  284. }
  285. }
  286. @objc func gestureOpenPdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  287. openPdfThumbnail()
  288. }
  289. @objc func gestureClosePdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  290. if recognizer.state == .recognized {
  291. closePdfThumbnail()
  292. }
  293. }
  294. // MARK: -
  295. func setConstraints() {
  296. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  297. UIView.animate(withDuration: animateDuration, animations: {
  298. if UIDevice.current.userInterfaceIdiom == .phone {
  299. self.pdfThumbnailScrollView.isHidden = true
  300. self.pdfThumbnailScrollViewleadingAnchor?.constant = -widthThumbnail
  301. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  302. self.pageViewLeftAnchor?.constant = 10
  303. } else {
  304. self.pdfThumbnailScrollViewleadingAnchor?.constant = 0
  305. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  306. self.pageViewLeftAnchor?.constant = widthThumbnail + 10
  307. }
  308. self.pdfThumbnailViewleadingAnchor?.constant = (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  309. self.view.layoutIfNeeded()
  310. self.pdfView.autoScales = true
  311. })
  312. }
  313. func openPdfThumbnail() {
  314. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  315. if UIDevice.current.userInterfaceIdiom == .phone && self.pdfThumbnailScrollView.isHidden {
  316. self.pdfThumbnailScrollView.isHidden = false
  317. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  318. UIView.animate(withDuration: animateDuration, animations: {
  319. self.pdfThumbnailScrollViewleadingAnchor?.constant = 0
  320. self.pageViewLeftAnchor?.constant = widthThumbnail + 10
  321. self.view.layoutIfNeeded()
  322. })
  323. }
  324. }
  325. func closePdfThumbnail() {
  326. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  327. if UIDevice.current.userInterfaceIdiom == .phone && !self.pdfThumbnailScrollView.isHidden {
  328. UIView.animate(withDuration: animateDuration) {
  329. self.pdfThumbnailScrollViewleadingAnchor?.constant = -widthThumbnail
  330. self.pageViewLeftAnchor?.constant = 10
  331. self.view.layoutIfNeeded()
  332. } completion: { _ in
  333. self.pdfThumbnailScrollView.isHidden = true
  334. }
  335. }
  336. }
  337. @objc func handlePageChange() {
  338. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  339. guard let totalPages = pdfView.document?.pageCount else { return }
  340. let visibleRect = CGRect(x: pdfThumbnailScrollView.contentOffset.x, y: pdfThumbnailScrollView.contentOffset.y, width: pdfThumbnailScrollView.bounds.size.width, height: pdfThumbnailScrollView.bounds.size.height)
  341. let centerPoint = CGPoint(x: visibleRect.size.width/2, y: visibleRect.size.height/2)
  342. let currentPageY = CGFloat(curPage) * thumbnailViewHeight + CGFloat(curPage) * thumbnailPadding
  343. var gotoY = currentPageY - centerPoint.y
  344. let startY = visibleRect.origin.y < 0 ? 0 : (visibleRect.origin.y + thumbnailViewHeight)
  345. let endY = visibleRect.origin.y + visibleRect.height + thumbnailViewHeight
  346. if currentPageY < startY {
  347. if gotoY < 0 { gotoY = 0 }
  348. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  349. } else if currentPageY > endY {
  350. if gotoY > pdfThumbnailView.frame.height - visibleRect.height {
  351. gotoY = pdfThumbnailView.frame.height - visibleRect.height
  352. }
  353. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  354. } else {
  355. print("visible")
  356. }
  357. pageView.alpha = 1
  358. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  359. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  360. UIView.animate(withDuration: 1.0, delay: 2.5, animations: {
  361. self.pageView.alpha = 0
  362. })
  363. }
  364. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  365. pdfSelection.color = .yellow
  366. pdfView.currentSelection = pdfSelection
  367. pdfView.go(to: pdfSelection)
  368. }
  369. private func selectPage(with label: String) {
  370. guard let pdf = pdfView.document else { return }
  371. if let pageNr = Int(label) {
  372. if pageNr > 0 && pageNr <= pdf.pageCount {
  373. if let page = pdf.page(at: pageNr - 1) {
  374. self.pdfView.go(to: page)
  375. }
  376. } else {
  377. let alertController = UIAlertController(title: NSLocalizedString("_invalid_page_", comment: ""),
  378. message: NSLocalizedString("_the_entered_page_number_doesn't_exist_", comment: ""),
  379. preferredStyle: .alert)
  380. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: nil))
  381. self.present(alertController, animated: true, completion: nil)
  382. }
  383. }
  384. }
  385. }