NCViewerImageZoom.swift 14 KB

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