NCViewerImageZoom.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 willAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  26. func didAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  27. func dismiss()
  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 detailViewBottomConstraint: NSLayoutConstraint!
  35. @IBOutlet weak var detailViewHeightConstraint: NSLayoutConstraint!
  36. @IBOutlet weak var scrollView: UIScrollView!
  37. @IBOutlet weak var imageView: UIImageView!
  38. @IBOutlet weak var statusViewImage: UIImageView!
  39. @IBOutlet weak var statusLabel: UILabel!
  40. @IBOutlet weak var detailView: NCViewerImageDetailView!
  41. var delegate: NCViewerImageZoomDelegate?
  42. var image: UIImage?
  43. var metadata: tableMetadata = tableMetadata()
  44. var index: Int = 0
  45. var minScale: CGFloat = 0
  46. var startY: CGFloat = 0
  47. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  48. var startPanLocation: CGPoint = CGPoint.zero
  49. var panDistanceClose: CGFloat = 0
  50. var panDistanceForDetailView: CGFloat = 0
  51. var defaultImageViewTopConstraint: CGFloat = 0
  52. var defaultImageViewBottomConstraint: CGFloat = 0
  53. var defaultDetailViewTopConstraint: CGFloat = 0
  54. var tempImageViewTopConstraint: CGFloat = 0
  55. var tempImageViewBottomConstraint: CGFloat = 0
  56. var openDetailView: Bool = false
  57. required init?(coder aDecoder: NSCoder) {
  58. super.init(coder: aDecoder)
  59. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  60. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  61. }
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. scrollView.delegate = self
  65. scrollView.contentInsetAdjustmentBehavior = .never
  66. view.addGestureRecognizer(doubleTapGestureRecognizer)
  67. if image == nil {
  68. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "noPreview"), width: view.frame.width, height: view.frame.width, color: .gray)
  69. }
  70. if let image = image {
  71. imageView.image = image
  72. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  73. }
  74. if NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) != nil {
  75. statusViewImage.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: .gray)
  76. statusLabel.text = "LIVE"
  77. } else {
  78. statusViewImage.image = nil
  79. statusLabel.text = ""
  80. }
  81. }
  82. override func viewWillAppear(_ animated: Bool) {
  83. super.viewWillAppear(animated)
  84. updateZoomScale()
  85. updateConstraints()
  86. startY = imageView.frame.origin.y
  87. panDistanceClose = view.bounds.height / 3
  88. panDistanceForDetailView = view.bounds.height / 4
  89. detailView.updateExifLocal(metadata: metadata)
  90. delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  91. }
  92. override func viewDidAppear(_ animated: Bool) {
  93. super.viewDidAppear(animated)
  94. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  95. }
  96. override func viewDidLayoutSubviews() {
  97. super.viewDidLayoutSubviews()
  98. updateZoomScale()
  99. updateConstraints()
  100. }
  101. //MARK: - Gesture
  102. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  103. let pointInView = gestureRecognizer.location(in: imageView)
  104. var newZoomScale = scrollView.maximumZoomScale
  105. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  106. newZoomScale = scrollView.minimumZoomScale
  107. }
  108. if newZoomScale > scrollView.maximumZoomScale {
  109. return
  110. }
  111. let width = scrollView.bounds.width / newZoomScale
  112. let height = scrollView.bounds.height / newZoomScale
  113. let originX = pointInView.x - (width / 2.0)
  114. let originY = pointInView.y - (height / 2.0)
  115. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  116. scrollView.zoom(to: rectToZoomTo, animated: true)
  117. }
  118. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  119. let currentLocation = gestureRecognizer.translation(in: self.view)
  120. switch gestureRecognizer.state {
  121. case .began:
  122. startPanLocation = currentLocation
  123. scrollView.isScrollEnabled = false
  124. tempImageViewTopConstraint = imageViewTopConstraint.constant
  125. tempImageViewBottomConstraint = imageViewBottomConstraint.constant
  126. case .ended:
  127. if !openDetailView {
  128. scrollView.isScrollEnabled = true
  129. imageViewTopConstraint.constant = defaultImageViewTopConstraint
  130. imageViewBottomConstraint.constant = defaultImageViewBottomConstraint
  131. }
  132. case .changed:
  133. let dy = currentLocation.y - startPanLocation.y
  134. print(dy)
  135. print(imageView.frame.origin.y)
  136. imageViewTopConstraint.constant = tempImageViewTopConstraint + dy
  137. imageViewBottomConstraint.constant = tempImageViewBottomConstraint - dy
  138. // DISMISS
  139. if imageView.frame.origin.y > panDistanceClose + startY {
  140. delegate?.dismiss()
  141. }
  142. // OPEN DETAIL
  143. if imageView.frame.origin.y < startY - panDistanceForDetailView {
  144. if !detailView.hasData() { return }
  145. detailViewHeightConstraint.constant = (view.frame.width / 3) * 2
  146. let offsetBottom = self.view.safeAreaInsets.bottom + 20
  147. detailViewBottomConstraint.constant = imageViewBottomConstraint.constant - offsetBottom
  148. openDetailView = true
  149. }
  150. // CLOSE DETAIL
  151. if imageView.frame.origin.y > startY - panDistanceForDetailView {
  152. defaultDetailViewConstraint()
  153. openDetailView = false
  154. }
  155. default:
  156. break
  157. }
  158. }
  159. //MARK: - Function
  160. func updateZoomScale() {
  161. let size = view.bounds.size
  162. let widthScale = size.width / imageView.bounds.width
  163. let heightScale = size.height / imageView.bounds.height
  164. minScale = min(widthScale, heightScale)
  165. scrollView.minimumZoomScale = minScale
  166. scrollView.zoomScale = minScale
  167. scrollView.maximumZoomScale = 1
  168. }
  169. func updateConstraints() {
  170. let size = view.bounds.size
  171. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  172. imageViewTopConstraint.constant = yOffset
  173. imageViewBottomConstraint.constant = yOffset
  174. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  175. imageViewLeadingConstraint.constant = xOffset
  176. imageViewTrailingConstraint.constant = xOffset
  177. defaultImageViewTopConstraint = imageViewTopConstraint.constant
  178. defaultImageViewBottomConstraint = imageViewBottomConstraint.constant
  179. defaultDetailViewConstraint()
  180. view.layoutIfNeeded()
  181. let contentHeight = yOffset * 2 + imageView.frame.height
  182. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  183. }
  184. func defaultDetailViewConstraint() {
  185. detailViewBottomConstraint.constant = -40
  186. openDetailView = false
  187. }
  188. }
  189. extension NCViewerImageZoom: UIScrollViewDelegate {
  190. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  191. return imageView
  192. }
  193. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  194. updateConstraints()
  195. }
  196. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  197. }
  198. }