NCViewerImageZoom.swift 13 KB

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