NCViewerImageZoom.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. import NCCommunication
  25. protocol NCViewerImageZoomDelegate {
  26. func photoPageViewController(_ viewerImageZoom: NCViewerImageZoom, scrollViewDidScroll scrollView: UIScrollView)
  27. func didAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  28. func willAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
  29. func dismissImageZoom()
  30. }
  31. class NCViewerImageZoom: UIViewController {
  32. @IBOutlet weak var detailViewConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var imageViewBottomConstraint: 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. @IBOutlet weak var detailView: NCViewerImageDetailView!
  40. @IBOutlet weak var videoToolBar: NCViewerVideoToolBar!
  41. var delegate: NCViewerImageZoomDelegate?
  42. var viewerImage: NCViewerImage?
  43. var image: UIImage?
  44. var metadata: tableMetadata = tableMetadata()
  45. var index: Int = 0
  46. var isShowDetail: Bool = false
  47. var noPreview: Bool = false
  48. var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
  49. var imageViewConstraint: CGFloat = 0
  50. // MARK: - View Life Cycle
  51. required init?(coder aDecoder: NSCoder) {
  52. super.init(coder: aDecoder)
  53. doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
  54. doubleTapGestureRecognizer.numberOfTapsRequired = 2
  55. }
  56. override func viewDidLoad() {
  57. super.viewDidLoad()
  58. scrollView.delegate = self
  59. scrollView.maximumZoomScale = 4
  60. scrollView.minimumZoomScale = 1
  61. view.addGestureRecognizer(doubleTapGestureRecognizer)
  62. if image == nil {
  63. var named = "noPreview"
  64. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue { named = "noPreviewAudio" }
  65. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue { named = "noPreviewVideo" }
  66. image = UIImage.init(named: named)!.image(color: .gray, size: view.frame.width)
  67. self.noPreview = true
  68. }
  69. if let image = image {
  70. imageView.image = image
  71. imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: image.size.width, height: image.size.height)
  72. }
  73. if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
  74. statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
  75. statusLabel.text = "LIVE"
  76. } else {
  77. statusViewImage.image = nil
  78. statusLabel.text = ""
  79. }
  80. var heightMap = (view.bounds.height / 3)
  81. if view.bounds.width < view.bounds.height {
  82. heightMap = (view.bounds.width / 3)
  83. }
  84. detailViewConstraint.constant = 0
  85. detailView.update(metadata: metadata, image: image, heightMap: heightMap)
  86. detailView.hide()
  87. }
  88. override func viewWillAppear(_ animated: Bool) {
  89. super.viewWillAppear(animated)
  90. delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  91. if isShowDetail {
  92. openDetail()
  93. } else {
  94. }
  95. }
  96. override func viewDidAppear(_ animated: Bool) {
  97. super.viewDidAppear(animated)
  98. delegate?.didAppearImageZoom(viewerImageZoom: self, metadata: metadata)
  99. }
  100. override func viewDidLayoutSubviews() {
  101. super.viewDidLayoutSubviews()
  102. // Turn landescape - portrait - landescape - portrait
  103. if detailView.isShow() {
  104. self.openDetail()
  105. }
  106. }
  107. //MARK: - Gesture
  108. @objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  109. if detailView.isShow() { return }
  110. // NO ZOOM for Audio / Video
  111. if (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) && !videoToolBar.isHidden {
  112. return
  113. }
  114. let pointInView = gestureRecognizer.location(in: self.imageView)
  115. var newZoomScale = self.scrollView.maximumZoomScale
  116. if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
  117. newZoomScale = self.scrollView.minimumZoomScale
  118. }
  119. let width = self.scrollView.bounds.width / newZoomScale
  120. let height = self.scrollView.bounds.height / newZoomScale
  121. let originX = pointInView.x - (width / 2.0)
  122. let originY = pointInView.y - (height / 2.0)
  123. let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
  124. self.scrollView.zoom(to: rectToZoomTo, animated: true)
  125. }
  126. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  127. let currentLocation = gestureRecognizer.translation(in: self.view)
  128. switch gestureRecognizer.state {
  129. case .began:
  130. print("began")
  131. case .ended:
  132. if detailView.isShow() {
  133. self.imageViewTopConstraint.constant = -imageViewConstraint
  134. self.imageViewBottomConstraint.constant = imageViewConstraint
  135. } else {
  136. self.imageViewTopConstraint.constant = 0
  137. self.imageViewBottomConstraint.constant = 0
  138. }
  139. case .changed:
  140. imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
  141. imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
  142. // DISMISS VIEW
  143. if detailView.isHidden && (currentLocation.y > 20) {
  144. delegate?.dismissImageZoom()
  145. }
  146. // CLOSE DETAIL
  147. if !detailView.isHidden && (currentLocation.y > 20) {
  148. self.closeDetail()
  149. gestureRecognizer.state = .ended
  150. }
  151. // OPEN DETAIL
  152. if detailView.isHidden && (currentLocation.y < -20) {
  153. self.openDetail()
  154. gestureRecognizer.state = .ended
  155. }
  156. default:
  157. break
  158. }
  159. }
  160. }
  161. //MARK: -
  162. extension NCViewerImageZoom {
  163. private func openDetail() {
  164. self.detailView.show(textColor: self.viewerImage?.textColor)
  165. if let image = imageView.image {
  166. let ratioW = imageView.frame.width / image.size.width
  167. let ratioH = imageView.frame.height / image.size.height
  168. let ratio = ratioW < ratioH ? ratioW : ratioH
  169. let imageHeight = image.size.height * ratio
  170. imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
  171. if imageViewConstraint < 0 { imageViewConstraint = 0 }
  172. }
  173. UIView.animate(withDuration: 0.3) {
  174. self.imageViewTopConstraint.constant = -self.imageViewConstraint
  175. self.imageViewBottomConstraint.constant = self.imageViewConstraint
  176. self.detailViewConstraint.constant = self.detailView.frame.height
  177. self.view.layoutIfNeeded()
  178. } completion: { (_) in
  179. }
  180. scrollView.pinchGestureRecognizer?.isEnabled = false
  181. }
  182. private func closeDetail() {
  183. self.detailView.hide()
  184. imageViewConstraint = 0
  185. UIView.animate(withDuration: 0.3) {
  186. self.imageViewTopConstraint.constant = 0
  187. self.imageViewBottomConstraint.constant = 0
  188. self.detailViewConstraint.constant = 0
  189. self.view.layoutIfNeeded()
  190. } completion: { (_) in
  191. }
  192. scrollView.pinchGestureRecognizer?.isEnabled = true
  193. }
  194. }
  195. //MARK: -
  196. extension NCViewerImageZoom: UIScrollViewDelegate {
  197. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  198. return imageView
  199. }
  200. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  201. if scrollView.zoomScale > 1 {
  202. if let image = imageView.image {
  203. let ratioW = imageView.frame.width / image.size.width
  204. let ratioH = imageView.frame.height / image.size.height
  205. let ratio = ratioW < ratioH ? ratioW : ratioH
  206. let newWidth = image.size.width * ratio
  207. let newHeight = image.size.height * ratio
  208. let conditionLeft = newWidth*scrollView.zoomScale > imageView.frame.width
  209. let left = 0.5 * (conditionLeft ? newWidth - imageView.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
  210. let conditioTop = newHeight*scrollView.zoomScale > imageView.frame.height
  211. let top = 0.5 * (conditioTop ? newHeight - imageView.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
  212. scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
  213. }
  214. } else {
  215. scrollView.contentInset = .zero
  216. }
  217. }
  218. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  219. self.delegate?.photoPageViewController(self, scrollViewDidScroll: scrollView)
  220. }
  221. }