NCViewerImageZoom.swift 12 KB

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