NCViewerImageZoom.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. protocol NCViewerImageZoomDelegate {
  25. func presentImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  26. func dismissImageZoom()
  27. }
  28. class NCViewerImageZoom: UIViewController {
  29. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var detailViewTopConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var scrollView: UIScrollView!
  35. @IBOutlet weak var imageView: UIImageView!
  36. @IBOutlet weak var statusViewImage: UIImageView!
  37. @IBOutlet weak var statusLabel: UILabel!
  38. @IBOutlet weak var detailView: NCViewerImageDetailView!
  39. var delegate: NCViewerImageZoomDelegate?
  40. var viewerImage: NCViewerImage?
  41. var image: UIImage?
  42. var metadata: tableMetadata = tableMetadata()
  43. var index: Int = 0
  44. var minScale: CGFloat = 0
  45. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  46. private var startImageViewTopConstraint: CGFloat = 0
  47. private var startImageViewBottomConstraint: CGFloat = 0
  48. private var startPoint = CGPoint.zero
  49. required init?(coder aDecoder: NSCoder) {
  50. super.init(coder: aDecoder)
  51. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  52. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  53. }
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. scrollView.delegate = self
  57. scrollView.contentInsetAdjustmentBehavior = .never
  58. view.addGestureRecognizer(doubleTapGestureRecognizer)
  59. if image == nil {
  60. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "noPreview"), width: view.frame.width, height: view.frame.width, color: .gray)
  61. }
  62. if let image = image {
  63. imageView.image = image
  64. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  65. }
  66. if NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) != nil {
  67. statusViewImage.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: .gray)
  68. statusLabel.text = "LIVE"
  69. } else {
  70. statusViewImage.image = nil
  71. statusLabel.text = ""
  72. }
  73. updateZoomScale()
  74. centreConstraints()
  75. }
  76. override func viewWillAppear(_ animated: Bool) {
  77. super.viewWillAppear(animated)
  78. updateZoomScale()
  79. centreConstraints()
  80. }
  81. override func viewDidAppear(_ animated: Bool) {
  82. super.viewDidAppear(animated)
  83. detailView.updateExifLocal(metadata: metadata)
  84. detailViewTopConstraint.constant = 0
  85. detailView.hide()
  86. updateZoomScale()
  87. centreConstraints()
  88. delegate?.presentImageZoom(viewerImageZoom: self, metadata: metadata)
  89. }
  90. override func viewDidLayoutSubviews() {
  91. super.viewDidLayoutSubviews()
  92. updateZoomScale()
  93. centreConstraints()
  94. }
  95. //MARK: - Gesture
  96. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  97. if detailView.isShow() { return }
  98. let pointInView = gestureRecognizer.location(in: imageView)
  99. var newZoomScale = scrollView.maximumZoomScale
  100. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  101. newZoomScale = scrollView.minimumZoomScale
  102. }
  103. if newZoomScale > scrollView.maximumZoomScale {
  104. return
  105. }
  106. let width = scrollView.bounds.width / newZoomScale
  107. let height = scrollView.bounds.height / newZoomScale
  108. let originX = pointInView.x - (width / 2.0)
  109. let originY = pointInView.y - (height / 2.0)
  110. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  111. scrollView.zoom(to: rectToZoomTo, animated: true)
  112. }
  113. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  114. let currentLocation = gestureRecognizer.translation(in: self.view)
  115. switch gestureRecognizer.state {
  116. case .began:
  117. startPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  118. // save start
  119. startImageViewTopConstraint = imageViewTopConstraint.constant
  120. startImageViewBottomConstraint = imageViewBottomConstraint.constant
  121. case .ended:
  122. if !detailView.isShow() {
  123. centreConstraints()
  124. }
  125. case .changed:
  126. let deltaY = startPoint.y - currentLocation.y
  127. print(deltaY)
  128. imageViewTopConstraint.constant = startImageViewTopConstraint + currentLocation.y
  129. imageViewBottomConstraint.constant = startImageViewBottomConstraint - currentLocation.y
  130. detailViewTopConstraint.constant = -imageViewBottomConstraint.constant
  131. // DISMISS
  132. if imageView.center.y > view.center.y + (view.bounds.height / 4) {
  133. delegate?.dismissImageZoom()
  134. }
  135. // OPEN DETAIL
  136. if imageView.center.y < view.center.y - 50 {
  137. if !detailView.hasData() { return }
  138. guard let textColor = self.viewerImage?.textColor else {return }
  139. var height = (view.bounds.height / 3) * 2
  140. if view.bounds.width < view.bounds.height {
  141. height = (view.bounds.width / 3) * 2
  142. }
  143. detailView.show(height: height, textColor: textColor)
  144. }
  145. // CLOSE DETAIL
  146. if imageView.center.y > view.center.y + 50 {
  147. if detailView.isShow() {
  148. detailView.hide()
  149. // gestureRecognizer.state = .ended
  150. }
  151. }
  152. default:
  153. break
  154. }
  155. }
  156. //MARK: - Function
  157. func updateZoomScale() {
  158. let size = view.bounds.size
  159. let widthScale = size.width / imageView.bounds.width
  160. let heightScale = size.height / imageView.bounds.height
  161. minScale = min(widthScale, heightScale)
  162. scrollView.minimumZoomScale = minScale
  163. scrollView.zoomScale = minScale
  164. scrollView.maximumZoomScale = 1
  165. }
  166. func centreConstraints() {
  167. let size = view.bounds.size
  168. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  169. imageViewTopConstraint.constant = yOffset
  170. imageViewBottomConstraint.constant = yOffset
  171. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  172. imageViewLeadingConstraint.constant = xOffset
  173. imageViewTrailingConstraint.constant = xOffset
  174. // reset detail
  175. detailViewTopConstraint.constant = 0
  176. detailView.hide()
  177. view.layoutIfNeeded()
  178. let contentHeight = yOffset * 2 + imageView.frame.height
  179. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  180. }
  181. }
  182. extension NCViewerImageZoom: UIScrollViewDelegate {
  183. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  184. return imageView
  185. }
  186. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  187. centreConstraints()
  188. }
  189. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  190. }
  191. }