NCViewerImageZoom.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. let pointInView = gestureRecognizer.location(in: imageView)
  130. var newZoomScale = scrollView.maximumZoomScale
  131. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  132. newZoomScale = scrollView.minimumZoomScale
  133. }
  134. if newZoomScale > scrollView.maximumZoomScale {
  135. return
  136. }
  137. let width = scrollView.bounds.width / newZoomScale
  138. let height = scrollView.bounds.height / newZoomScale
  139. let originX = pointInView.x - (width / 2.0)
  140. let originY = pointInView.y - (height / 2.0)
  141. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  142. scrollView.zoom(to: rectToZoomTo, animated: true)
  143. }
  144. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  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.isHidden = true
  155. case .ended:
  156. if !detailView.isShow() {
  157. UIView.animate(withDuration: 0.3) {
  158. self.centreConstraints()
  159. } completion: { (_) in
  160. self.updateZoomScale()
  161. self.centreConstraints()
  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.isHidden = true
  176. } else {
  177. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  178. self.videoToolBar.isHidden = false
  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. func updateZoomScale() {
  224. let size = view.bounds.size
  225. let widthScale = size.width / imageView.bounds.width
  226. let heightScale = size.height / imageView.bounds.height
  227. minScale = min(widthScale, heightScale)
  228. scrollView.minimumZoomScale = minScale
  229. scrollView.zoomScale = minScale
  230. scrollView.maximumZoomScale = 1
  231. }
  232. func centreConstraints() {
  233. let size = view.bounds.size
  234. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  235. imageViewTopConstraint.constant = yOffset
  236. imageViewBottomConstraint.constant = yOffset
  237. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  238. imageViewLeadingConstraint.constant = xOffset
  239. imageViewTrailingConstraint.constant = xOffset
  240. // reset detail
  241. detailViewTopConstraint.constant = 0
  242. detailView.hide()
  243. view.layoutIfNeeded()
  244. let contentHeight = yOffset * 2 + imageView.frame.height
  245. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  246. }
  247. }
  248. extension NCViewerImageZoom: UIScrollViewDelegate {
  249. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  250. return imageView
  251. }
  252. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  253. centreConstraints()
  254. }
  255. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  256. }
  257. }