NCViewerImageZoom.swift 11 KB

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