NCViewerImageZoom.swift 11 KB

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