NCViewerImageZoom.swift 12 KB

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