NCViewerImageZoom.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 willAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  29. func dismissImageZoom()
  30. }
  31. class NCViewerImageZoom: UIViewController {
  32. @IBOutlet weak var detailViewTopConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  35. @IBOutlet weak var scrollView: UIScrollView!
  36. @IBOutlet weak var imageView: UIImageView!
  37. @IBOutlet weak var statusViewImage: UIImageView!
  38. @IBOutlet weak var statusLabel: UILabel!
  39. @IBOutlet weak var detailView: NCViewerImageDetailView!
  40. @IBOutlet weak var videoToolBar: NCViewerVideoToolBar!
  41. var delegate: NCViewerImageZoomDelegate?
  42. var viewerImage: NCViewerImage?
  43. var image: UIImage?
  44. var metadata: tableMetadata = tableMetadata()
  45. var index: Int = 0
  46. var minScale: CGFloat = 0
  47. var noPreview: Bool = false
  48. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  49. private var startImageViewTopConstraint: CGFloat = 0
  50. private var startImageViewBottomConstraint: CGFloat = 0
  51. private var startPoint = CGPoint.zero
  52. private var topPoint = CGPoint.zero
  53. // MARK: - View Life Cycle
  54. required init?(coder aDecoder: NSCoder) {
  55. super.init(coder: aDecoder)
  56. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  57. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. scrollView.delegate = self
  62. scrollView.maximumZoomScale = 4
  63. scrollView.minimumZoomScale = 1
  64. view.addGestureRecognizer(doubleTapGestureRecognizer)
  65. if image == nil {
  66. var named = "noPreview"
  67. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue { named = "noPreviewAudio" }
  68. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue { named = "noPreviewVideo" }
  69. image = UIImage.init(named: named)!.image(color: .gray, size: view.frame.width)
  70. self.noPreview = true
  71. }
  72. if let image = image {
  73. imageView.image = image
  74. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  75. }
  76. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  77. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  78. statusLabel.text = "LIVE"
  79. } else {
  80. statusViewImage.image = nil
  81. statusLabel.text = ""
  82. }
  83. }
  84. override func viewWillAppear(_ animated: Bool) {
  85. super.viewWillAppear(animated)
  86. if !detailView.isShow() {
  87. // updateZoom()
  88. // updateConstraints()
  89. }
  90. delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  91. }
  92. override func viewDidAppear(_ animated: Bool) {
  93. super.viewDidAppear(animated)
  94. var heightMap = (view.bounds.height / 3)
  95. if view.bounds.width < view.bounds.height {
  96. heightMap = (view.bounds.width / 3)
  97. }
  98. if !detailView.isShow() {
  99. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  100. detailViewTopConstraint.constant = 0
  101. detailView.hide()
  102. // updateZoom()
  103. // updateConstraints()
  104. }
  105. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  106. }
  107. override func viewDidDisappear(_ animated: Bool) {
  108. super.viewDidDisappear(animated)
  109. }
  110. override func viewDidLayoutSubviews() {
  111. super.viewDidLayoutSubviews()
  112. }
  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. // NO INFO for Audio / Video
  134. // if viewerImage?.viewerVideo?.player?.rate == 1 { return }
  135. let currentLocation = gestureRecognizer.translation(in: self.view)
  136. switch gestureRecognizer.state {
  137. case .began:
  138. startPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  139. topPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  140. // save start
  141. startImageViewTopConstraint = imageViewTopConstraint.constant
  142. startImageViewBottomConstraint = imageViewBottomConstraint.constant
  143. case .ended:
  144. if !detailView.isShow() {
  145. UIView.animate(withDuration: 0.3) {
  146. // self.updateConstraints()
  147. } completion: { (_) in
  148. // self.updateZoom()
  149. // self.updateConstraints()
  150. }
  151. } else if detailView.isSavedContraint() {
  152. UIView.animate(withDuration: 0.3) {
  153. self.imageViewTopConstraint.constant = self.detailView.imageViewTopConstraintConstant
  154. self.imageViewBottomConstraint.constant = self.detailView.imageViewBottomConstraintConstant
  155. self.detailViewTopConstraint.constant = self.detailView.detailViewTopConstraintConstant
  156. self.view.layoutIfNeeded()
  157. } completion: { (_) in
  158. }
  159. }
  160. case .changed:
  161. if currentLocation.y < topPoint.y { topPoint = currentLocation }
  162. let deltaY = startPoint.y - currentLocation.y
  163. imageViewTopConstraint.constant = startImageViewTopConstraint + currentLocation.y
  164. imageViewBottomConstraint.constant = startImageViewBottomConstraint - currentLocation.y
  165. detailViewTopConstraint.constant = -imageViewBottomConstraint.constant
  166. // DISMISS
  167. if imageView.center.y > view.center.y + 100 {
  168. delegate?.dismissImageZoom()
  169. }
  170. // OPEN DETAIL
  171. if imageView.center.y < view.center.y - 30 {
  172. if detailView.isHidden {
  173. detailView.show(textColor: self.viewerImage?.textColor)
  174. gestureRecognizer.state = .ended
  175. UIView.animate(withDuration: 0.3) {
  176. self.imageViewTopConstraint.constant = self.startImageViewTopConstraint - self.detailView.frame.height
  177. self.imageViewBottomConstraint.constant = self.startImageViewBottomConstraint + self.detailView.frame.height
  178. self.detailViewTopConstraint.constant = -self.imageViewBottomConstraint.constant
  179. self.view.layoutIfNeeded()
  180. } completion: { (_) in
  181. // Save detail constraints
  182. self.detailView.imageViewTopConstraintConstant = self.imageViewTopConstraint.constant
  183. self.detailView.imageViewBottomConstraintConstant = self.imageViewBottomConstraint.constant
  184. self.detailView.detailViewTopConstraintConstant = self.detailViewTopConstraint.constant
  185. }
  186. }
  187. //detailView.show(textColor: self.viewerImage?.textColor)
  188. }
  189. // CLOSE DETAIL
  190. if (imageView.center.y > view.center.y + 30) || (deltaY < -30) || (topPoint.y + 30 < currentLocation.y) {
  191. if detailView.isShow() {
  192. detailView.hide()
  193. gestureRecognizer.state = .ended
  194. }
  195. }
  196. default:
  197. break
  198. }
  199. }
  200. }
  201. extension NCViewerImageZoom: UIScrollViewDelegate {
  202. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  203. return imageView
  204. }
  205. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  206. if scrollView.zoomScale > 1 {
  207. if let image = imageView.image {
  208. let ratioW = imageView.frame.width / image.size.width
  209. let ratioH = imageView.frame.height / image.size.height
  210. let ratio = ratioW < ratioH ? ratioW : ratioH
  211. let newWidth = image.size.width * ratio
  212. let newHeight = image.size.height * ratio
  213. let conditionLeft = newWidth*scrollView.zoomScale > imageView.frame.width
  214. let left = 0.5 * (conditionLeft ? newWidth - imageView.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  215. let conditioTop = newHeight*scrollView.zoomScale > imageView.frame.height
  216. let top = 0.5 * (conditioTop ? newHeight - imageView.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  217. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  218. }
  219. } else {
  220. scrollView.contentInset = .zero
  221. }
  222. }
  223. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  224. self.delegate?.photoPageViewController(self, scrollViewDidScroll: scrollView)
  225. }
  226. }