NCViewerImageZoom.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 detailViewTopConstraint: 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 viewerImage: NCViewerImage?
  43. var image: UIImage?
  44. var metadata: tableMetadata = tableMetadata()
  45. var index: Int = 0
  46. var minScale: CGFloat = 0
  47. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  48. private var startImageViewTopConstraint: CGFloat = 0
  49. private var startImageViewBottomConstraint: CGFloat = 0
  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.contentInsetAdjustmentBehavior = .never
  59. view.addGestureRecognizer(doubleTapGestureRecognizer)
  60. if image == nil {
  61. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "noPreview"), width: view.frame.width, height: view.frame.width, color: .gray)
  62. }
  63. if let image = image {
  64. imageView.image = image
  65. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  66. }
  67. if NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) != nil {
  68. statusViewImage.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: .gray)
  69. statusLabel.text = "LIVE"
  70. } else {
  71. statusViewImage.image = nil
  72. statusLabel.text = ""
  73. }
  74. }
  75. override func viewWillAppear(_ animated: Bool) {
  76. super.viewWillAppear(animated)
  77. updateZoomScale()
  78. updateConstraints()
  79. //detailView.updateExifLocal(metadata: metadata)
  80. detailViewHeightConstraint.constant = view.bounds.width
  81. detailViewTopConstraint.constant = 0
  82. detailView.hide()
  83. delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  84. }
  85. override func viewDidAppear(_ animated: Bool) {
  86. super.viewDidAppear(animated)
  87. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  88. }
  89. override func viewDidLayoutSubviews() {
  90. super.viewDidLayoutSubviews()
  91. updateZoomScale()
  92. updateConstraints()
  93. }
  94. //MARK: - Gesture
  95. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  96. if detailView.isShow() { return }
  97. let pointInView = gestureRecognizer.location(in: imageView)
  98. var newZoomScale = scrollView.maximumZoomScale
  99. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  100. newZoomScale = scrollView.minimumZoomScale
  101. }
  102. if newZoomScale > scrollView.maximumZoomScale {
  103. return
  104. }
  105. let width = scrollView.bounds.width / newZoomScale
  106. let height = scrollView.bounds.height / newZoomScale
  107. let originX = pointInView.x - (width / 2.0)
  108. let originY = pointInView.y - (height / 2.0)
  109. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  110. scrollView.zoom(to: rectToZoomTo, animated: true)
  111. }
  112. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  113. if !detailView.hasData() { return }
  114. let currentLocation = gestureRecognizer.translation(in: self.view)
  115. switch gestureRecognizer.state {
  116. case .began:
  117. scrollView.isScrollEnabled = false
  118. // save start
  119. startImageViewTopConstraint = imageViewTopConstraint.constant
  120. startImageViewBottomConstraint = imageViewBottomConstraint.constant
  121. case .ended:
  122. if !detailView.isShow() {
  123. updateConstraints()
  124. scrollView.isScrollEnabled = true
  125. }
  126. case .changed:
  127. imageViewTopConstraint.constant = startImageViewTopConstraint + currentLocation.y
  128. imageViewBottomConstraint.constant = startImageViewBottomConstraint - currentLocation.y
  129. detailViewTopConstraint.constant = -imageViewBottomConstraint.constant
  130. // DISMISS
  131. if imageView.center.y > view.center.y + 150 {
  132. delegate?.dismiss()
  133. }
  134. // OPEN DETAIL
  135. if imageView.center.y < view.center.y - 50 {
  136. if let textColor = self.viewerImage?.textColor {
  137. detailView.show(textColor: textColor)
  138. }
  139. }
  140. // CLOSE DETAIL
  141. if imageView.center.y > view.center.y + 50 {
  142. if detailView.isShow() {
  143. detailView.hide()
  144. gestureRecognizer.state = .ended
  145. }
  146. }
  147. default:
  148. break
  149. }
  150. }
  151. //MARK: - Function
  152. func updateZoomScale() {
  153. let size = view.bounds.size
  154. let widthScale = size.width / imageView.bounds.width
  155. let heightScale = size.height / imageView.bounds.height
  156. minScale = min(widthScale, heightScale)
  157. scrollView.minimumZoomScale = minScale
  158. scrollView.zoomScale = minScale
  159. scrollView.maximumZoomScale = 1
  160. }
  161. func updateConstraints() {
  162. let size = view.bounds.size
  163. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  164. imageViewTopConstraint.constant = yOffset
  165. imageViewBottomConstraint.constant = yOffset
  166. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  167. imageViewLeadingConstraint.constant = xOffset
  168. imageViewTrailingConstraint.constant = xOffset
  169. // reset detail
  170. detailViewTopConstraint.constant = 0
  171. detailView.hide()
  172. view.layoutIfNeeded()
  173. let contentHeight = yOffset * 2 + imageView.frame.height
  174. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  175. }
  176. }
  177. extension NCViewerImageZoom: UIScrollViewDelegate {
  178. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  179. return imageView
  180. }
  181. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  182. updateConstraints()
  183. }
  184. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  185. }
  186. }