NCViewerImageZoom.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. protocol NCViewerImageZoomDelegate {
  25. func didAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  26. func willAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  27. func dismissImageZoom()
  28. }
  29. class NCViewerImageZoom: UIViewController {
  30. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var detailViewTopConstraint: 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. var delegate: NCViewerImageZoomDelegate?
  41. var viewerImage: NCViewerImage?
  42. var image: UIImage?
  43. var metadata: tableMetadata = tableMetadata()
  44. var index: Int = 0
  45. var minScale: CGFloat = 0
  46. var noPreview: Bool = false
  47. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  48. private var startImageViewTopConstraint: CGFloat = 0
  49. private var startImageViewBottomConstraint: CGFloat = 0
  50. private var startPoint = CGPoint.zero
  51. private var topPoint = CGPoint.zero
  52. required init?(coder aDecoder: NSCoder) {
  53. super.init(coder: aDecoder)
  54. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  55. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  56. }
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. scrollView.delegate = self
  60. scrollView.contentInsetAdjustmentBehavior = .never
  61. view.addGestureRecognizer(doubleTapGestureRecognizer)
  62. if image == nil {
  63. var named = "noPreview"
  64. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileAudio { named = "noPreviewAudio" }
  65. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo { named = "noPreviewVideo" }
  66. image = CCGraphics.changeThemingColorImage(UIImage.init(named: named), width: view.frame.width, height: view.frame.width, color: .gray)
  67. self.noPreview = true
  68. }
  69. if let image = image {
  70. imageView.image = image
  71. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  72. }
  73. if NCManageDatabase.shared.isLivePhoto(metadata: metadata) != nil {
  74. statusViewImage.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: .gray)
  75. statusLabel.text = "LIVE"
  76. } else {
  77. statusViewImage.image = nil
  78. statusLabel.text = ""
  79. }
  80. updateZoomScale()
  81. centreConstraints()
  82. }
  83. override func viewWillAppear(_ animated: Bool) {
  84. super.viewWillAppear(animated)
  85. if !detailView.isShow() {
  86. updateZoomScale()
  87. centreConstraints()
  88. }
  89. delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  90. }
  91. override func viewDidAppear(_ animated: Bool) {
  92. super.viewDidAppear(animated)
  93. var heightMap = (view.bounds.height / 3)
  94. if view.bounds.width < view.bounds.height {
  95. heightMap = (view.bounds.width / 3)
  96. }
  97. if !detailView.isShow() {
  98. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  99. detailViewTopConstraint.constant = 0
  100. detailView.hide()
  101. updateZoomScale()
  102. centreConstraints()
  103. }
  104. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  105. }
  106. override func viewDidDisappear(_ animated: Bool) {
  107. super.viewDidDisappear(animated)
  108. if detailView.isShow() {
  109. detailView.hide()
  110. updateZoomScale()
  111. centreConstraints()
  112. }
  113. }
  114. override func viewDidLayoutSubviews() {
  115. super.viewDidLayoutSubviews()
  116. updateZoomScale()
  117. centreConstraints()
  118. }
  119. //MARK: - Gesture
  120. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  121. if detailView.isShow() { return }
  122. let pointInView = gestureRecognizer.location(in: imageView)
  123. var newZoomScale = scrollView.maximumZoomScale
  124. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  125. newZoomScale = scrollView.minimumZoomScale
  126. }
  127. if newZoomScale > scrollView.maximumZoomScale {
  128. return
  129. }
  130. let width = scrollView.bounds.width / newZoomScale
  131. let height = scrollView.bounds.height / newZoomScale
  132. let originX = pointInView.x - (width / 2.0)
  133. let originY = pointInView.y - (height / 2.0)
  134. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  135. scrollView.zoom(to: rectToZoomTo, animated: true)
  136. }
  137. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  138. let currentLocation = gestureRecognizer.translation(in: self.view)
  139. switch gestureRecognizer.state {
  140. case .began:
  141. startPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  142. topPoint = CGPoint(x: currentLocation.x, y: currentLocation.y)
  143. // save start
  144. startImageViewTopConstraint = imageViewTopConstraint.constant
  145. startImageViewBottomConstraint = imageViewBottomConstraint.constant
  146. case .ended:
  147. if !detailView.isShow() {
  148. UIView.animate(withDuration: 0.3) {
  149. self.centreConstraints()
  150. } completion: { (_) in
  151. self.updateZoomScale()
  152. self.centreConstraints()
  153. }
  154. }
  155. case .changed:
  156. if currentLocation.y < topPoint.y { topPoint = currentLocation }
  157. let deltaY = startPoint.y - currentLocation.y
  158. imageViewTopConstraint.constant = startImageViewTopConstraint + currentLocation.y
  159. imageViewBottomConstraint.constant = startImageViewBottomConstraint - currentLocation.y
  160. detailViewTopConstraint.constant = -imageViewBottomConstraint.constant
  161. // DISMISS
  162. if imageView.center.y > view.center.y + 100 {
  163. delegate?.dismissImageZoom()
  164. }
  165. // OPEN DETAIL
  166. if imageView.center.y < view.center.y - 30 {
  167. if detailView.isHidden {
  168. detailView.show(textColor: self.viewerImage?.textColor)
  169. gestureRecognizer.state = .ended
  170. UIView.animate(withDuration: 0.3) {
  171. self.imageViewTopConstraint.constant = self.startImageViewTopConstraint - self.detailView.frame.height
  172. self.imageViewBottomConstraint.constant = self.startImageViewBottomConstraint + self.detailView.frame.height
  173. self.detailViewTopConstraint.constant = -self.imageViewBottomConstraint.constant
  174. self.view.layoutIfNeeded()
  175. } completion: { (_) in
  176. //end.
  177. }
  178. }
  179. detailView.show(textColor: self.viewerImage?.textColor)
  180. }
  181. // CLOSE DETAIL
  182. if (imageView.center.y > view.center.y + 30) || (deltaY < -30) || (topPoint.y + 30 < currentLocation.y) {
  183. if detailView.isShow() {
  184. detailView.hide()
  185. gestureRecognizer.state = .ended
  186. }
  187. }
  188. default:
  189. break
  190. }
  191. }
  192. //MARK: - Function
  193. func updateZoomScale() {
  194. let size = view.bounds.size
  195. let widthScale = size.width / imageView.bounds.width
  196. let heightScale = size.height / imageView.bounds.height
  197. minScale = min(widthScale, heightScale)
  198. scrollView.minimumZoomScale = minScale
  199. scrollView.zoomScale = minScale
  200. scrollView.maximumZoomScale = 1
  201. }
  202. func centreConstraints() {
  203. let size = view.bounds.size
  204. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  205. imageViewTopConstraint.constant = yOffset
  206. imageViewBottomConstraint.constant = yOffset
  207. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  208. imageViewLeadingConstraint.constant = xOffset
  209. imageViewTrailingConstraint.constant = xOffset
  210. // reset detail
  211. detailViewTopConstraint.constant = 0
  212. detailView.hide()
  213. view.layoutIfNeeded()
  214. let contentHeight = yOffset * 2 + imageView.frame.height
  215. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  216. }
  217. }
  218. extension NCViewerImageZoom: UIScrollViewDelegate {
  219. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  220. return imageView
  221. }
  222. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  223. centreConstraints()
  224. }
  225. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  226. }
  227. }