NCViewerImageZoom.swift 9.2 KB

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