NCViewerImageZoom.swift 6.2 KB

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