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 dismissImageZoom()
  28. }
  29. class NCViewerImageZoom: UIViewController {
  30. @IBOutlet weak var detailViewConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var imageViewBottomConstraint: 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 isShowDetail: Bool = false
  45. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  46. var imageViewConstraint: CGFloat = 0
  47. // MARK: - View Life Cycle
  48. required init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  51. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  52. }
  53. deinit {
  54. print("")
  55. }
  56. override func viewDidLoad() {
  57. super.viewDidLoad()
  58. scrollView.delegate = self
  59. scrollView.maximumZoomScale = 4
  60. scrollView.minimumZoomScale = 1
  61. view.addGestureRecognizer(doubleTapGestureRecognizer)
  62. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  63. if image == nil {
  64. image = UIImage.init(named: "noPreviewVideo")!.image(color: .gray, size: view.frame.width)
  65. }
  66. // Show Video Toolbar
  67. if !metadata.livePhoto {
  68. videoToolBar.isHidden = false
  69. }
  70. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  71. NCViewerVideo.shared.initVideoPlayer(imageView: self.imageView, viewerVideoToolBar: self.videoToolBar, metadata: self.metadata)
  72. }
  73. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  74. if image == nil {
  75. image = UIImage.init(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
  76. }
  77. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  78. NCViewerVideo.shared.initVideoPlayer(imageView: self.imageView, viewerVideoToolBar: self.videoToolBar, metadata: self.metadata)
  79. }
  80. } else {
  81. if image == nil {
  82. image = UIImage.init(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  83. }
  84. }
  85. imageView.image = image
  86. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  87. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  88. statusLabel.text = "LIVE"
  89. } else {
  90. statusViewImage.image = nil
  91. statusLabel.text = ""
  92. }
  93. var heightMap = (view.bounds.height / 3)
  94. if view.bounds.width < view.bounds.height {
  95. heightMap = (view.bounds.width / 3)
  96. }
  97. detailViewConstraint.constant = 0
  98. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  99. detailView.hide()
  100. }
  101. override func viewDidAppear(_ animated: Bool) {
  102. super.viewDidAppear(animated)
  103. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  104. }
  105. override func viewDidDisappear(_ animated: Bool) {
  106. super.viewDidDisappear(animated)
  107. // NCViewerVideo.shared.videoStop()
  108. }
  109. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  110. super.viewWillTransition(to: size, with: coordinator)
  111. coordinator.animate(alongsideTransition: { (context) in
  112. // back to the original size
  113. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  114. self.view.layoutIfNeeded()
  115. UIView.animate(withDuration: context.transitionDuration) {
  116. // resize frame video
  117. NCViewerVideo.shared.videoLayer?.frame = self.imageView.layer.bounds
  118. // resize detail
  119. if self.detailView.isShow() {
  120. self.openDetail()
  121. }
  122. }
  123. }) { (_) in }
  124. }
  125. func reload(image: UIImage, metadata: tableMetadata) {
  126. imageView.image = image
  127. self.metadata = metadata
  128. }
  129. //MARK: - Gesture
  130. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  131. if detailView.isShow() { return }
  132. // NO ZOOM for Audio / Video
  133. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && !videoToolBar.isHidden {
  134. return
  135. }
  136. let pointInView = gestureRecognizer.location(in: self.imageView)
  137. var newZoomScale = self.scrollView.maximumZoomScale
  138. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  139. newZoomScale = self.scrollView.minimumZoomScale
  140. }
  141. let width = self.scrollView.bounds.width / newZoomScale
  142. let height = self.scrollView.bounds.height / newZoomScale
  143. let originX = pointInView.x - (width / 2.0)
  144. let originY = pointInView.y - (height / 2.0)
  145. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  146. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  147. }
  148. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  149. let currentLocation = gestureRecognizer.translation(in: self.view)
  150. switch gestureRecognizer.state {
  151. case .began:
  152. print("began")
  153. case .ended:
  154. if detailView.isShow() {
  155. self.imageViewTopConstraint.constant = -imageViewConstraint
  156. self.imageViewBottomConstraint.constant = imageViewConstraint
  157. } else {
  158. self.imageViewTopConstraint.constant = 0
  159. self.imageViewBottomConstraint.constant = 0
  160. }
  161. case .changed:
  162. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  163. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  164. // DISMISS VIEW
  165. if detailView.isHidden && (currentLocation.y > 20) {
  166. delegate?.dismissImageZoom()
  167. gestureRecognizer.state = .ended
  168. }
  169. // CLOSE DETAIL
  170. if !detailView.isHidden && (currentLocation.y > 20) {
  171. self.closeDetail()
  172. gestureRecognizer.state = .ended
  173. }
  174. // OPEN DETAIL
  175. if detailView.isHidden && (currentLocation.y < -20) {
  176. self.openDetail()
  177. gestureRecognizer.state = .ended
  178. }
  179. default:
  180. break
  181. }
  182. }
  183. }
  184. //MARK: -
  185. extension NCViewerImageZoom {
  186. private func openDetail() {
  187. self.detailView.show(textColor: self.viewerImage?.textColor)
  188. if let image = imageView.image {
  189. let ratioW = imageView.frame.width / image.size.width
  190. let ratioH = imageView.frame.height / image.size.height
  191. let ratio = ratioW < ratioH ? ratioW : ratioH
  192. let imageHeight = image.size.height * ratio
  193. imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  194. if imageViewConstraint < 0 { imageViewConstraint = 0 }
  195. }
  196. UIView.animate(withDuration: 0.3) {
  197. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  198. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  199. self.detailViewConstraint.constant = self.detailView.frame.height
  200. self.view.layoutIfNeeded()
  201. } completion: { (_) in
  202. }
  203. scrollView.pinchGestureRecognizer?.isEnabled = false
  204. videoToolBar.hideToolBar()
  205. }
  206. private func closeDetail() {
  207. self.detailView.hide()
  208. imageViewConstraint = 0
  209. UIView.animate(withDuration: 0.3) {
  210. self.imageViewTopConstraint.constant = 0
  211. self.imageViewBottomConstraint.constant = 0
  212. self.detailViewConstraint.constant = 0
  213. self.view.layoutIfNeeded()
  214. } completion: { (_) in
  215. }
  216. scrollView.pinchGestureRecognizer?.isEnabled = true
  217. if !metadata.livePhoto {
  218. videoToolBar.showToolBar()
  219. }
  220. }
  221. }
  222. //MARK: -
  223. extension NCViewerImageZoom: UIScrollViewDelegate {
  224. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  225. return imageView
  226. }
  227. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  228. if scrollView.zoomScale > 1 {
  229. if let image = imageView.image {
  230. let ratioW = imageView.frame.width / image.size.width
  231. let ratioH = imageView.frame.height / image.size.height
  232. let ratio = ratioW < ratioH ? ratioW : ratioH
  233. let newWidth = image.size.width * ratio
  234. let newHeight = image.size.height * ratio
  235. let conditionLeft = newWidth*scrollView.zoomScale > imageView.frame.width
  236. let left = 0.5 * (conditionLeft ? newWidth - imageView.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  237. let conditioTop = newHeight*scrollView.zoomScale > imageView.frame.height
  238. let top = 0.5 * (conditioTop ? newHeight - imageView.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  239. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  240. }
  241. } else {
  242. scrollView.contentInset = .zero
  243. }
  244. }
  245. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  246. }
  247. }