NCViewerImageZoom.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. // VideoToolBar
  122. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  123. self.videoToolBar.isHidden = false
  124. }
  125. }
  126. //MARK: - Gesture
  127. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  128. if detailView.isShow() { return }
  129. // NO ZOOM for Audio / Video
  130. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  131. let pointInView = gestureRecognizer.location(in: imageView)
  132. var newZoomScale = scrollView.maximumZoomScale
  133. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  134. newZoomScale = scrollView.minimumZoomScale
  135. }
  136. if newZoomScale > scrollView.maximumZoomScale {
  137. return
  138. }
  139. let width = scrollView.bounds.width / newZoomScale
  140. let height = scrollView.bounds.height / newZoomScale
  141. let originX = pointInView.x - (width / 2.0)
  142. let originY = pointInView.y - (height / 2.0)
  143. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  144. scrollView.zoom(to: rectToZoomTo, animated: true)
  145. }
  146. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  147. // NO INFO for Audio / Video
  148. if viewerImage?.viewerVideo?.player?.rate == 1 { return }
  149. let currentLocation = gestureRecognizer.translation(in: self.view)
  150. switch gestureRecognizer.state {
  151. case .began:
  152. startPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  153. topPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  154. // save start
  155. startImageViewTopConstraint = imageViewTopConstraint.constant
  156. startImageViewBottomConstraint = imageViewBottomConstraint.constant
  157. // VideoToolBar
  158. self.videoToolBar.isHidden = true
  159. case .ended:
  160. if !detailView.isShow() {
  161. UIView.animate(withDuration: 0.3) {
  162. self.centreConstraints()
  163. } completion: { (_) in
  164. self.updateZoomScale()
  165. self.centreConstraints()
  166. }
  167. } else if detailView.isSavedContraint() {
  168. UIView.animate(withDuration: 0.3) {
  169. self.imageViewTopConstraint.constant = self.detailView.imageViewTopConstraintConstant
  170. self.imageViewBottomConstraint.constant = self.detailView.imageViewBottomConstraintConstant
  171. self.detailViewTopConstraint.constant = self.detailView.detailViewTopConstraintConstant
  172. self.view.layoutIfNeeded()
  173. } completion: { (_) in
  174. }
  175. }
  176. // VideoToolBar
  177. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  178. if detailView.isShow() {
  179. self.videoToolBar.isHidden = true
  180. } else {
  181. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  182. self.videoToolBar.isHidden = false
  183. }
  184. }
  185. }
  186. case .changed:
  187. if currentLocation.y < topPoint.y { topPoint = currentLocation }
  188. let deltaY = startPoint.y - currentLocation.y
  189. imageViewTopConstraint.constant = startImageViewTopConstraint + currentLocation.y
  190. imageViewBottomConstraint.constant = startImageViewBottomConstraint - currentLocation.y
  191. detailViewTopConstraint.constant = -imageViewBottomConstraint.constant
  192. // DISMISS
  193. if imageView.center.y > view.center.y + 100 {
  194. delegate?.dismissImageZoom()
  195. }
  196. // OPEN DETAIL
  197. if imageView.center.y < view.center.y - 30 {
  198. if detailView.isHidden {
  199. detailView.show(textColor: self.viewerImage?.textColor)
  200. gestureRecognizer.state = .ended
  201. UIView.animate(withDuration: 0.3) {
  202. self.imageViewTopConstraint.constant = self.startImageViewTopConstraint - self.detailView.frame.height
  203. self.imageViewBottomConstraint.constant = self.startImageViewBottomConstraint + self.detailView.frame.height
  204. self.detailViewTopConstraint.constant = -self.imageViewBottomConstraint.constant
  205. self.view.layoutIfNeeded()
  206. } completion: { (_) in
  207. // Save detail constraints
  208. self.detailView.imageViewTopConstraintConstant = self.imageViewTopConstraint.constant
  209. self.detailView.imageViewBottomConstraintConstant = self.imageViewBottomConstraint.constant
  210. self.detailView.detailViewTopConstraintConstant = self.detailViewTopConstraint.constant
  211. }
  212. }
  213. //detailView.show(textColor: self.viewerImage?.textColor)
  214. }
  215. // CLOSE DETAIL
  216. if (imageView.center.y > view.center.y + 30) || (deltaY < -30) || (topPoint.y + 30 < currentLocation.y) {
  217. if detailView.isShow() {
  218. detailView.hide()
  219. gestureRecognizer.state = .ended
  220. }
  221. }
  222. default:
  223. break
  224. }
  225. }
  226. //MARK: - Function
  227. func updateZoomScale() {
  228. let size = view.bounds.size
  229. let widthScale = size.width / imageView.bounds.width
  230. let heightScale = size.height / imageView.bounds.height
  231. minScale = min(widthScale, heightScale)
  232. scrollView.minimumZoomScale = minScale
  233. scrollView.zoomScale = minScale
  234. scrollView.maximumZoomScale = 1
  235. }
  236. func centreConstraints() {
  237. let size = view.bounds.size
  238. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  239. imageViewTopConstraint.constant = yOffset
  240. imageViewBottomConstraint.constant = yOffset
  241. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  242. imageViewLeadingConstraint.constant = xOffset
  243. imageViewTrailingConstraint.constant = xOffset
  244. // reset detail
  245. detailViewTopConstraint.constant = 0
  246. detailView.hide()
  247. view.layoutIfNeeded()
  248. let contentHeight = yOffset * 2 + imageView.frame.height
  249. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  250. }
  251. }
  252. extension NCViewerImageZoom: UIScrollViewDelegate {
  253. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  254. return imageView
  255. }
  256. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  257. centreConstraints()
  258. }
  259. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  260. }
  261. }