NCViewerImageZoom.swift 8.2 KB

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