NCViewerImageZoom.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // NCViewerImageZoom.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/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. class NCViewerImageZoom: UIViewController {
  25. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  26. @IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
  27. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  28. @IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var scrollView: UIScrollView!
  30. @IBOutlet weak var imageView: UIImageView!
  31. @IBOutlet weak var statusViewImage: UIImageView!
  32. weak var delegate: NCViewerImagePageContainer?
  33. var image: UIImage = UIImage()
  34. var metadata: tableMetadata = tableMetadata()
  35. var index: Int = 0
  36. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  37. required init?(coder aDecoder: NSCoder) {
  38. super.init(coder: aDecoder)
  39. self.doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  40. self.doubleTapGestureRecognizer.numberOfTapsRequired = 2
  41. }
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. scrollView.delegate = self
  45. scrollView.contentInsetAdjustmentBehavior = .never
  46. imageView.image = self.image
  47. imageView.frame = CGRect(x: self.imageView.frame.origin.x, y: self.imageView.frame.origin.y, width: self.image.size.width, height: self.image.size.height)
  48. view.addGestureRecognizer(self.doubleTapGestureRecognizer)
  49. }
  50. override func viewWillAppear(_ animated: Bool) {
  51. super.viewWillAppear(animated)
  52. delegate?.navigationItem.title = metadata.fileNameView
  53. }
  54. override func viewDidAppear(_ animated: Bool) {
  55. super.viewDidAppear(animated)
  56. updateZoomScaleForSize(view.bounds.size)
  57. updateConstraintsForSize(view.bounds.size)
  58. }
  59. override func viewDidLayoutSubviews() {
  60. super.viewDidLayoutSubviews()
  61. updateZoomScaleForSize(view.bounds.size)
  62. updateConstraintsForSize(view.bounds.size)
  63. }
  64. //MARK: - Gesture
  65. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  66. let pointInView = gestureRecognizer.location(in: self.imageView)
  67. var newZoomScale = self.scrollView.maximumZoomScale
  68. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  69. newZoomScale = scrollView.minimumZoomScale
  70. }
  71. let width = scrollView.bounds.width / newZoomScale
  72. let height = scrollView.bounds.height / newZoomScale
  73. let originX = pointInView.x - (width / 2.0)
  74. let originY = pointInView.y - (height / 2.0)
  75. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  76. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  77. }
  78. //MARK: - Function
  79. fileprivate func updateZoomScaleForSize(_ size: CGSize) {
  80. let widthScale = size.width / imageView.bounds.width
  81. let heightScale = size.height / imageView.bounds.height
  82. let minScale = min(widthScale, heightScale)
  83. scrollView.minimumZoomScale = minScale
  84. scrollView.zoomScale = minScale
  85. scrollView.maximumZoomScale = minScale * 4
  86. }
  87. fileprivate func updateConstraintsForSize(_ size: CGSize) {
  88. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  89. imageViewTopConstraint.constant = yOffset
  90. imageViewBottomConstraint.constant = yOffset
  91. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  92. imageViewLeadingConstraint.constant = xOffset
  93. imageViewTrailingConstraint.constant = xOffset
  94. let contentHeight = yOffset * 2 + self.imageView.frame.height
  95. view.layoutIfNeeded()
  96. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  97. }
  98. }
  99. extension NCViewerImageZoom: UIScrollViewDelegate {
  100. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  101. return imageView
  102. }
  103. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  104. updateConstraintsForSize(self.view.bounds.size)
  105. }
  106. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  107. }
  108. }