NCViewerImageZoom.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 didAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  26. func willAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  27. func dismissImageZoom()
  28. }
  29. class NCViewerImageZoom: UIViewController {
  30. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var detailViewTopConstraint: NSLayoutConstraint!
  35. @IBOutlet weak var scrollView: UIScrollView!
  36. @IBOutlet weak var imageView: UIImageView!
  37. @IBOutlet weak var statusViewImage: UIImageView!
  38. @IBOutlet weak var statusLabel: UILabel!
  39. @IBOutlet weak var detailView: NCViewerImageDetailView!
  40. var delegate: NCViewerImageZoomDelegate?
  41. var viewerImage: NCViewerImage?
  42. var image: UIImage?
  43. var metadata: tableMetadata = tableMetadata()
  44. var index: Int = 0
  45. var minScale: CGFloat = 0
  46. var noPreview: Bool = false
  47. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  48. private var startImageViewTopConstraint: CGFloat = 0
  49. private var startImageViewBottomConstraint: CGFloat = 0
  50. private var startPoint = CGPoint.zero
  51. private var topPoint = CGPoint.zero
  52. // MARK: - View Life Cycle
  53. required init?(coder aDecoder: NSCoder) {
  54. super.init(coder: aDecoder)
  55. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  56. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  57. }
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. scrollView.delegate = self
  61. scrollView.contentInsetAdjustmentBehavior = .never
  62. view.addGestureRecognizer(doubleTapGestureRecognizer)
  63. if image == nil {
  64. var named = "noPreview"
  65. if metadata.classFile == NCGlobal.shared.metadataClassFileAudio { named = "noPreviewAudio" }
  66. if metadata.classFile == NCGlobal.shared.metadataClassVideo { named = "noPreviewVideo" }
  67. image = UIImage.init(named: named)!.image(color: .gray, size: view.frame.width)
  68. self.noPreview = true
  69. }
  70. if let image = image {
  71. imageView.image = image
  72. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  73. }
  74. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  75. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  76. statusLabel.text = "LIVE"
  77. } else {
  78. statusViewImage.image = nil
  79. statusLabel.text = ""
  80. }
  81. updateZoomScale()
  82. centreConstraints()
  83. }
  84. override func viewWillAppear(_ animated: Bool) {
  85. super.viewWillAppear(animated)
  86. if !detailView.isShow() {
  87. updateZoomScale()
  88. centreConstraints()
  89. }
  90. delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  91. }
  92. override func viewDidAppear(_ animated: Bool) {
  93. super.viewDidAppear(animated)
  94. var heightMap = (view.bounds.height / 3)
  95. if view.bounds.width < view.bounds.height {
  96. heightMap = (view.bounds.width / 3)
  97. }
  98. if !detailView.isShow() {
  99. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  100. detailViewTopConstraint.constant = 0
  101. detailView.hide()
  102. updateZoomScale()
  103. centreConstraints()
  104. }
  105. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  106. }
  107. override func viewDidDisappear(_ animated: Bool) {
  108. super.viewDidDisappear(animated)
  109. if detailView.isShow() {
  110. detailView.hide()
  111. updateZoomScale()
  112. centreConstraints()
  113. }
  114. }
  115. override func viewDidLayoutSubviews() {
  116. super.viewDidLayoutSubviews()
  117. updateZoomScale()
  118. centreConstraints()
  119. }
  120. //MARK: - Gesture
  121. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  122. if detailView.isShow() { return }
  123. let pointInView = gestureRecognizer.location(in: imageView)
  124. var newZoomScale = scrollView.maximumZoomScale
  125. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  126. newZoomScale = scrollView.minimumZoomScale
  127. }
  128. if newZoomScale > scrollView.maximumZoomScale {
  129. return
  130. }
  131. let width = scrollView.bounds.width / newZoomScale
  132. let height = scrollView.bounds.height / newZoomScale
  133. let originX = pointInView.x - (width / 2.0)
  134. let originY = pointInView.y - (height / 2.0)
  135. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  136. scrollView.zoom(to: rectToZoomTo, animated: true)
  137. }
  138. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  139. let currentLocation = gestureRecognizer.translation(in: self.view)
  140. switch gestureRecognizer.state {
  141. case .began:
  142. startPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  143. topPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  144. // save start
  145. startImageViewTopConstraint = imageViewTopConstraint.constant
  146. startImageViewBottomConstraint = imageViewBottomConstraint.constant
  147. case .ended:
  148. if !detailView.isShow() {
  149. UIView.animate(withDuration: 0.3) {
  150. self.centreConstraints()
  151. } completion: { (_) in
  152. self.updateZoomScale()
  153. self.centreConstraints()
  154. }
  155. } else if detailView.isSavedContraint() {
  156. UIView.animate(withDuration: 0.3) {
  157. self.imageViewTopConstraint.constant = self.detailView.imageViewTopConstraintConstant
  158. self.imageViewBottomConstraint.constant = self.detailView.imageViewBottomConstraintConstant
  159. self.detailViewTopConstraint.constant = self.detailView.detailViewTopConstraintConstant
  160. self.view.layoutIfNeeded()
  161. } completion: { (_) in
  162. }
  163. }
  164. case .changed:
  165. if currentLocation.y < topPoint.y { topPoint = currentLocation }
  166. let deltaY = startPoint.y - currentLocation.y
  167. imageViewTopConstraint.constant = startImageViewTopConstraint + currentLocation.y
  168. imageViewBottomConstraint.constant = startImageViewBottomConstraint - currentLocation.y
  169. detailViewTopConstraint.constant = -imageViewBottomConstraint.constant
  170. // DISMISS
  171. if imageView.center.y > view.center.y + 100 {
  172. delegate?.dismissImageZoom()
  173. }
  174. // OPEN DETAIL
  175. if imageView.center.y < view.center.y - 30 {
  176. if detailView.isHidden {
  177. detailView.show(textColor: self.viewerImage?.textColor)
  178. gestureRecognizer.state = .ended
  179. UIView.animate(withDuration: 0.3) {
  180. self.imageViewTopConstraint.constant = self.startImageViewTopConstraint - self.detailView.frame.height
  181. self.imageViewBottomConstraint.constant = self.startImageViewBottomConstraint + self.detailView.frame.height
  182. self.detailViewTopConstraint.constant = -self.imageViewBottomConstraint.constant
  183. self.view.layoutIfNeeded()
  184. } completion: { (_) in
  185. // Save detail constraints
  186. self.detailView.imageViewTopConstraintConstant = self.imageViewTopConstraint.constant
  187. self.detailView.imageViewBottomConstraintConstant = self.imageViewBottomConstraint.constant
  188. self.detailView.detailViewTopConstraintConstant = self.detailViewTopConstraint.constant
  189. }
  190. }
  191. //detailView.show(textColor: self.viewerImage?.textColor)
  192. }
  193. // CLOSE DETAIL
  194. if (imageView.center.y > view.center.y + 30) || (deltaY < -30) || (topPoint.y + 30 < currentLocation.y) {
  195. if detailView.isShow() {
  196. detailView.hide()
  197. gestureRecognizer.state = .ended
  198. }
  199. }
  200. default:
  201. break
  202. }
  203. }
  204. //MARK: - Function
  205. func updateZoomScale() {
  206. let size = view.bounds.size
  207. let widthScale = size.width / imageView.bounds.width
  208. let heightScale = size.height / imageView.bounds.height
  209. minScale = min(widthScale, heightScale)
  210. scrollView.minimumZoomScale = minScale
  211. scrollView.zoomScale = minScale
  212. scrollView.maximumZoomScale = 1
  213. }
  214. func centreConstraints() {
  215. let size = view.bounds.size
  216. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  217. imageViewTopConstraint.constant = yOffset
  218. imageViewBottomConstraint.constant = yOffset
  219. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  220. imageViewLeadingConstraint.constant = xOffset
  221. imageViewTrailingConstraint.constant = xOffset
  222. // reset detail
  223. detailViewTopConstraint.constant = 0
  224. detailView.hide()
  225. view.layoutIfNeeded()
  226. let contentHeight = yOffset * 2 + imageView.frame.height
  227. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  228. }
  229. }
  230. extension NCViewerImageZoom: UIScrollViewDelegate {
  231. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  232. return imageView
  233. }
  234. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  235. centreConstraints()
  236. }
  237. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  238. }
  239. }