NCViewerPDF.swift 22 KB

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