NCViewerImageZoom.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. }
  28. class NCViewerImageZoom: UIViewController {
  29. @IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
  30. @IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var detailViewBottomConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var scrollView: UIScrollView!
  35. @IBOutlet weak var imageView: UIImageView!
  36. @IBOutlet weak var statusViewImage: UIImageView!
  37. @IBOutlet weak var statusLabel: UILabel!
  38. var delegate: NCViewerImageZoomDelegate?
  39. var image: UIImage?
  40. var metadata: tableMetadata = tableMetadata()
  41. var index: Int = 0
  42. var minScale: CGFloat = 0
  43. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  44. var defaultImageViewTopConstraint: CGFloat = 0
  45. var defaultImageViewBottomConstraint: CGFloat = 0
  46. var defaultDetailViewTopConstraint: CGFloat = 0
  47. var openDetailView: Bool = false
  48. required init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. self.doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  51. self.doubleTapGestureRecognizer.numberOfTapsRequired = 2
  52. }
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. scrollView.delegate = self
  56. scrollView.contentInsetAdjustmentBehavior = .never
  57. if image == nil {
  58. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "noPreview"), width: view.frame.width, height: view.frame.width, color: .gray)
  59. }
  60. if let image = image {
  61. imageView.image = image
  62. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  63. }
  64. if NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) != nil {
  65. statusViewImage.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: .gray)
  66. statusLabel.text = "LIVE"
  67. } else {
  68. statusViewImage.image = nil
  69. statusLabel.text = ""
  70. }
  71. view.addGestureRecognizer(doubleTapGestureRecognizer)
  72. }
  73. override func viewWillAppear(_ animated: Bool) {
  74. super.viewWillAppear(animated)
  75. updateZoomScale()
  76. updateConstraints()
  77. delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  78. }
  79. override func viewDidAppear(_ animated: Bool) {
  80. super.viewDidAppear(animated)
  81. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  82. }
  83. override func viewDidLayoutSubviews() {
  84. super.viewDidLayoutSubviews()
  85. updateZoomScale()
  86. updateConstraints()
  87. }
  88. //MARK: - Gesture
  89. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  90. let pointInView = gestureRecognizer.location(in: imageView)
  91. var newZoomScale = scrollView.maximumZoomScale
  92. if scrollView.zoomScale >= newZoomScale || abs(scrollView.zoomScale - newZoomScale) <= 0.01 {
  93. newZoomScale = scrollView.minimumZoomScale
  94. }
  95. if newZoomScale > scrollView.maximumZoomScale {
  96. return
  97. }
  98. let width = scrollView.bounds.width / newZoomScale
  99. let height = scrollView.bounds.height / newZoomScale
  100. let originX = pointInView.x - (width / 2.0)
  101. let originY = pointInView.y - (height / 2.0)
  102. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  103. scrollView.zoom(to: rectToZoomTo, animated: true)
  104. }
  105. //MARK: - Function
  106. func updateZoomScale() {
  107. let size = view.bounds.size
  108. let widthScale = size.width / imageView.bounds.width
  109. let heightScale = size.height / imageView.bounds.height
  110. minScale = min(widthScale, heightScale)
  111. scrollView.minimumZoomScale = minScale
  112. scrollView.zoomScale = minScale
  113. scrollView.maximumZoomScale = 1
  114. }
  115. func updateConstraints() {
  116. let size = view.bounds.size
  117. let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  118. imageViewTopConstraint.constant = yOffset
  119. imageViewBottomConstraint.constant = yOffset
  120. let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  121. imageViewLeadingConstraint.constant = xOffset
  122. imageViewTrailingConstraint.constant = xOffset
  123. defaultImageViewTopConstraint = imageViewTopConstraint.constant
  124. defaultImageViewBottomConstraint = imageViewBottomConstraint.constant
  125. defaultDetailViewConstraint()
  126. view.layoutIfNeeded()
  127. let contentHeight = yOffset * 2 + imageView.frame.height
  128. scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: contentHeight)
  129. }
  130. func defaultDetailViewConstraint() {
  131. detailViewBottomConstraint.constant = -40
  132. openDetailView = false
  133. }
  134. }
  135. extension NCViewerImageZoom: UIScrollViewDelegate {
  136. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  137. return imageView
  138. }
  139. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  140. updateConstraints()
  141. }
  142. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  143. }
  144. }