NCViewerMediaZoom.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // NCViewerMediaZoom.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 NCViewerMediaZoomDelegate {
  26. func dismissImageZoom()
  27. }
  28. class NCViewerMediaZoom: UIViewController {
  29. @IBOutlet weak var detailViewConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var scrollView: UIScrollView!
  33. @IBOutlet weak var imageVideoContainer: imageVideoContainerView!
  34. @IBOutlet weak var statusViewImage: UIImageView!
  35. @IBOutlet weak var statusLabel: UILabel!
  36. @IBOutlet weak var detailView: NCViewerMediaDetailView!
  37. @IBOutlet weak var playerToolBar: NCPlayerToolBar!
  38. var delegate: NCViewerMediaZoomDelegate?
  39. var viewerMedia: NCViewerMedia?
  40. var ncplayer: NCPlayer?
  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("deinit NCViewerMediaZoom")
  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. playerToolBar.isHidden = false
  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. } else {
  75. if image == nil {
  76. image = UIImage.init(named: "noPreview")!.image(color: .gray, size: view.frame.width)
  77. }
  78. }
  79. imageVideoContainer.image = image
  80. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  81. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  82. statusLabel.text = "LIVE"
  83. } else {
  84. statusViewImage.image = nil
  85. statusLabel.text = ""
  86. }
  87. var heightMap = (view.bounds.height / 3)
  88. if view.bounds.width < view.bounds.height {
  89. heightMap = (view.bounds.width / 3)
  90. }
  91. detailViewConstraint.constant = 0
  92. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  93. detailView.hide()
  94. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue, let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  95. self.ncplayer = NCPlayer.init(url: url)
  96. self.viewerMedia?.ncplayer = self.ncplayer
  97. }
  98. }
  99. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  100. super.viewWillTransition(to: size, with: coordinator)
  101. coordinator.animate(alongsideTransition: { (context) in
  102. // back to the original size
  103. self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
  104. self.view.layoutIfNeeded()
  105. UIView.animate(withDuration: context.transitionDuration) {
  106. // resize detail
  107. if self.detailView.isShow() {
  108. self.openDetail()
  109. }
  110. }
  111. }) { (_) in }
  112. }
  113. func reload(image: UIImage, metadata: tableMetadata) {
  114. imageVideoContainer.image = image
  115. self.metadata = metadata
  116. }
  117. //MARK: - Gesture
  118. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  119. if detailView.isShow() { return }
  120. // NO ZOOM for Audio / Video
  121. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && !playerToolBar.isHidden {
  122. return
  123. }
  124. let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
  125. var newZoomScale = self.scrollView.maximumZoomScale
  126. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  127. newZoomScale = self.scrollView.minimumZoomScale
  128. }
  129. let width = self.scrollView.bounds.width / newZoomScale
  130. let height = self.scrollView.bounds.height / newZoomScale
  131. let originX = pointInView.x - (width / 2.0)
  132. let originY = pointInView.y - (height / 2.0)
  133. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  134. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  135. }
  136. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  137. let currentLocation = gestureRecognizer.translation(in: self.view)
  138. switch gestureRecognizer.state {
  139. case .began:
  140. print("began")
  141. case .ended:
  142. if detailView.isShow() {
  143. self.imageViewTopConstraint.constant = -imageViewConstraint
  144. self.imageViewBottomConstraint.constant = imageViewConstraint
  145. } else {
  146. self.imageViewTopConstraint.constant = 0
  147. self.imageViewBottomConstraint.constant = 0
  148. }
  149. case .changed:
  150. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  151. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  152. // DISMISS VIEW
  153. if detailView.isHidden && (currentLocation.y > 20) {
  154. delegate?.dismissImageZoom()
  155. gestureRecognizer.state = .ended
  156. }
  157. // CLOSE DETAIL
  158. if !detailView.isHidden && (currentLocation.y > 20) {
  159. self.closeDetail()
  160. gestureRecognizer.state = .ended
  161. }
  162. // OPEN DETAIL
  163. if detailView.isHidden && (currentLocation.y < -20) {
  164. self.openDetail()
  165. gestureRecognizer.state = .ended
  166. }
  167. default:
  168. break
  169. }
  170. }
  171. }
  172. //MARK: -
  173. extension NCViewerMediaZoom {
  174. private func openDetail() {
  175. self.detailView.show(textColor: self.viewerMedia?.textColor)
  176. if let image = imageVideoContainer.image {
  177. let ratioW = imageVideoContainer.frame.width / image.size.width
  178. let ratioH = imageVideoContainer.frame.height / image.size.height
  179. let ratio = ratioW < ratioH ? ratioW : ratioH
  180. let imageHeight = image.size.height * ratio
  181. imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  182. if imageViewConstraint < 0 { imageViewConstraint = 0 }
  183. }
  184. UIView.animate(withDuration: 0.3) {
  185. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  186. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  187. self.detailViewConstraint.constant = self.detailView.frame.height
  188. self.view.layoutIfNeeded()
  189. } completion: { (_) in
  190. }
  191. scrollView.pinchGestureRecognizer?.isEnabled = false
  192. playerToolBar.hideToolBar()
  193. }
  194. private func closeDetail() {
  195. self.detailView.hide()
  196. imageViewConstraint = 0
  197. UIView.animate(withDuration: 0.3) {
  198. self.imageViewTopConstraint.constant = 0
  199. self.imageViewBottomConstraint.constant = 0
  200. self.detailViewConstraint.constant = 0
  201. self.view.layoutIfNeeded()
  202. } completion: { (_) in
  203. }
  204. scrollView.pinchGestureRecognizer?.isEnabled = true
  205. playerToolBar.showToolBar(metadata: metadata, detailView: nil)
  206. }
  207. }
  208. //MARK: -
  209. extension NCViewerMediaZoom: UIScrollViewDelegate {
  210. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  211. return imageVideoContainer
  212. }
  213. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  214. if scrollView.zoomScale > 1 {
  215. if let image = imageVideoContainer.image {
  216. let ratioW = imageVideoContainer.frame.width / image.size.width
  217. let ratioH = imageVideoContainer.frame.height / image.size.height
  218. let ratio = ratioW < ratioH ? ratioW : ratioH
  219. let newWidth = image.size.width * ratio
  220. let newHeight = image.size.height * ratio
  221. let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
  222. let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  223. let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
  224. let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  225. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  226. }
  227. } else {
  228. scrollView.contentInset = .zero
  229. }
  230. }
  231. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  232. }
  233. }
  234. //MARK: -
  235. class imageVideoContainerView: UIImageView {
  236. var playerLayer: CALayer?
  237. override func layoutSublayers(of layer: CALayer) {
  238. super.layoutSublayers(of: layer)
  239. playerLayer?.frame = self.bounds
  240. }
  241. }