NCViewerImageContentView.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // NCViewerImageContentView.swift
  3. // Nextcloud
  4. //
  5. // Created by Suraj Thomas K on 7/10/18 Copyright © 2018 Al Tayer Group LLC..
  6. // Modify for Nextcloud by Marino Faggiana on 04/03/2020.
  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. public struct ZoomScale {
  24. public var minimumZoomScale: CGFloat
  25. public var maximumZoomScale: CGFloat
  26. public static let `default` = ZoomScale(
  27. minimum: 1.0,
  28. maximum: 5.0
  29. )
  30. public static let identity = ZoomScale(
  31. minimum: 1.0,
  32. maximum: 1.0
  33. )
  34. public init(minimum: CGFloat, maximum: CGFloat) {
  35. minimumZoomScale = minimum
  36. maximumZoomScale = maximum
  37. }
  38. }
  39. public class NCViewerImageContentView: UIScrollView {
  40. // MARK: - Exposed variables
  41. var metadata = tableMetadata()
  42. internal static var interItemSpacing: CGFloat = 0.0
  43. internal var index: Int {
  44. didSet {
  45. resetZoom()
  46. }
  47. }
  48. internal static var contentTransformer: NCViewerImageContentTransformer = NCViewerImageDefaultContentTransformers.horizontalMoveInOut
  49. internal var position: CGFloat {
  50. didSet {
  51. updateTransform()
  52. }
  53. }
  54. internal var image: UIImage? {
  55. didSet {
  56. updateImageView()
  57. }
  58. }
  59. internal var isLoading: Bool = false {
  60. didSet {
  61. indicatorContainer.isHidden = !isLoading
  62. if isLoading {
  63. indicator.startAnimating()
  64. } else {
  65. indicator.stopAnimating()
  66. }
  67. }
  68. }
  69. internal var zoomLevels: ZoomScale? {
  70. didSet {
  71. zoomScale = ZoomScale.default.minimumZoomScale
  72. minimumZoomScale = zoomLevels?.minimumZoomScale ?? ZoomScale.default.minimumZoomScale
  73. maximumZoomScale = zoomLevels?.maximumZoomScale ?? ZoomScale.default.maximumZoomScale
  74. }
  75. }
  76. // MARK: - Private enumerations
  77. private enum Constants {
  78. static let indicatorViewSize: CGFloat = 60.0
  79. }
  80. // MARK: - Private variables
  81. private lazy var imageView: UIImageView = {
  82. let imageView = UIImageView()
  83. imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  84. imageView.contentMode = .scaleAspectFit
  85. imageView.clipsToBounds = true
  86. return imageView
  87. }()
  88. private lazy var indicator: UIActivityIndicatorView = {
  89. let indicatorView = UIActivityIndicatorView()
  90. indicatorView.style = .whiteLarge
  91. indicatorView.color = .darkGray
  92. indicatorView.hidesWhenStopped = true
  93. return indicatorView
  94. }()
  95. private lazy var indicatorContainer: UIView = {
  96. let container = UIView()
  97. container.backgroundColor = .clear
  98. container.layer.cornerRadius = Constants.indicatorViewSize * 0.5
  99. container.layer.masksToBounds = true
  100. return container
  101. }()
  102. private lazy var doubleTapGestureRecognizer: UITapGestureRecognizer = { [unowned self] in
  103. let gesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap(_:)))
  104. gesture.numberOfTapsRequired = 2
  105. gesture.numberOfTouchesRequired = 1
  106. return gesture
  107. }()
  108. init(index itemIndex: Int, position: CGFloat, frame: CGRect) {
  109. self.index = itemIndex
  110. self.position = position
  111. super.init(frame: frame)
  112. initializeViewComponents()
  113. }
  114. required init?(coder aDecoder: NSCoder) {
  115. fatalError("Do nto use `init?(coder:)`")
  116. }
  117. }
  118. // MARK: - View Composition and Events
  119. extension NCViewerImageContentView {
  120. private func initializeViewComponents() {
  121. addSubview(imageView)
  122. imageView.frame = frame
  123. setupIndicatorView()
  124. configureScrollView()
  125. addGestureRecognizer(doubleTapGestureRecognizer)
  126. updateTransform()
  127. }
  128. private func configureScrollView() {
  129. isMultipleTouchEnabled = true
  130. showsHorizontalScrollIndicator = false
  131. showsVerticalScrollIndicator = false
  132. contentSize = imageView.bounds.size
  133. canCancelContentTouches = false
  134. zoomLevels = ZoomScale.default
  135. delegate = self
  136. bouncesZoom = false
  137. }
  138. private func resetZoom() {
  139. setZoomScale(1.0, animated: false)
  140. imageView.transform = CGAffineTransform.identity
  141. contentSize = imageView.frame.size
  142. contentOffset = .zero
  143. }
  144. private func setupIndicatorView() {
  145. addSubview(indicatorContainer)
  146. indicatorContainer.translatesAutoresizingMaskIntoConstraints = false
  147. NSLayoutConstraint.activate([
  148. indicatorContainer.widthAnchor.constraint(equalToConstant: Constants.indicatorViewSize),
  149. indicatorContainer.heightAnchor.constraint(equalToConstant: Constants.indicatorViewSize),
  150. indicatorContainer.centerXAnchor.constraint(equalTo: centerXAnchor),
  151. indicatorContainer.centerYAnchor.constraint(equalTo: centerYAnchor)
  152. ])
  153. indicatorContainer.addSubview(indicator)
  154. indicator.translatesAutoresizingMaskIntoConstraints = false
  155. NSLayoutConstraint.activate([
  156. indicator.leadingAnchor.constraint(equalTo: indicatorContainer.leadingAnchor),
  157. indicator.trailingAnchor.constraint(equalTo: indicatorContainer.trailingAnchor),
  158. indicator.topAnchor.constraint(equalTo: indicatorContainer.topAnchor),
  159. indicator.bottomAnchor.constraint(equalTo: indicatorContainer.bottomAnchor)
  160. ])
  161. indicatorContainer.setNeedsLayout()
  162. indicatorContainer.layoutIfNeeded()
  163. indicatorContainer.isHidden = true
  164. }
  165. internal func updateTransform() {
  166. NCViewerImageContentView.contentTransformer(self, position)
  167. }
  168. internal func handleChangeInViewSize(to size: CGSize) {
  169. let oldScale = zoomScale
  170. zoomScale = 1.0
  171. imageView.frame = CGRect(origin: .zero, size: size)
  172. updateImageView()
  173. updateTransform()
  174. setZoomScale(oldScale, animated: false)
  175. contentSize = imageView.frame.size
  176. }
  177. func zoomScaleOne() {
  178. if zoomScale == 1 { return }
  179. let width = bounds.size.width
  180. let height = bounds.size.height
  181. let zoomRect = CGRect(
  182. x: bounds.size.width/2 - width * 0.5,
  183. y: bounds.size.height/2 - height * 0.5,
  184. width: width,
  185. height: height
  186. )
  187. zoom(to: zoomRect, animated: true)
  188. }
  189. @objc private func didDoubleTap(_ recognizer: UITapGestureRecognizer) {
  190. let locationInImage = recognizer.location(in: imageView)
  191. let isImageCoveringScreen = imageView.frame.size.width > bounds.size.width &&
  192. imageView.frame.size.height > bounds.size.height
  193. let zoomTo = (isImageCoveringScreen || zoomScale == maximumZoomScale) ? minimumZoomScale : maximumZoomScale
  194. guard zoomTo != zoomScale else {
  195. return
  196. }
  197. let width = bounds.size.width / zoomTo
  198. let height = bounds.size.height / zoomTo
  199. let zoomRect = CGRect(
  200. x: locationInImage.x - width * 0.5,
  201. y: locationInImage.y - height * 0.5,
  202. width: width,
  203. height: height
  204. )
  205. zoom(to: zoomRect, animated: true)
  206. }
  207. }
  208. // MARK: - UIScrollViewDelegate
  209. extension NCViewerImageContentView: UIScrollViewDelegate {
  210. public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  211. let shouldAllowZoom = (image != nil && position == 0.0)
  212. return shouldAllowZoom ? imageView : nil
  213. }
  214. public func scrollViewDidZoom(_ scrollView: UIScrollView) {
  215. centerImageView()
  216. }
  217. private func centerImageView() {
  218. var imageViewFrame = imageView.frame
  219. if imageViewFrame.size.width < bounds.size.width {
  220. imageViewFrame.origin.x = (bounds.size.width - imageViewFrame.size.width) / 2.0
  221. } else {
  222. imageViewFrame.origin.x = 0.0
  223. }
  224. if imageViewFrame.size.height < bounds.size.height {
  225. imageViewFrame.origin.y = (bounds.size.height - imageViewFrame.size.height) / 2.0
  226. } else {
  227. imageViewFrame.origin.y = 0.0
  228. }
  229. imageView.frame = imageViewFrame
  230. }
  231. private func updateImageView() {
  232. imageView.image = image
  233. if let contentImage = image {
  234. let imageViewSize = bounds.size
  235. let imageSize = contentImage.size
  236. var targetImageSize = imageViewSize
  237. if imageSize.width / imageSize.height > imageViewSize.width / imageViewSize.height {
  238. targetImageSize.height = imageViewSize.width / imageSize.width * imageSize.height
  239. } else {
  240. targetImageSize.width = imageViewSize.height / imageSize.height * imageSize.width
  241. }
  242. imageView.frame = CGRect(origin: .zero, size: targetImageSize)
  243. }
  244. centerImageView()
  245. }
  246. }