NCViewerImageZoom.swift 11 KB

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