NCViewerImageContentView.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.hidesWhenStopped = true
  92. return indicatorView
  93. }()
  94. private lazy var indicatorContainer: UIView = {
  95. let container = UIView()
  96. container.backgroundColor = NCBrandColor.sharedInstance.brand
  97. container.layer.cornerRadius = Constants.indicatorViewSize * 0.5
  98. container.layer.masksToBounds = true
  99. return container
  100. }()
  101. private lazy var doubleTapGestureRecognizer: UITapGestureRecognizer = { [unowned self] in
  102. let gesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap(_:)))
  103. gesture.numberOfTapsRequired = 2
  104. gesture.numberOfTouchesRequired = 1
  105. return gesture
  106. }()
  107. init(index itemIndex: Int, position: CGFloat, frame: CGRect) {
  108. self.index = itemIndex
  109. self.position = position
  110. super.init(frame: frame)
  111. initializeViewComponents()
  112. }
  113. required init?(coder aDecoder: NSCoder) {
  114. fatalError("Do nto use `init?(coder:)`")
  115. }
  116. }
  117. // MARK: - View Composition and Events
  118. extension NCViewerImageContentView {
  119. private func initializeViewComponents() {
  120. addSubview(imageView)
  121. imageView.frame = frame
  122. setupIndicatorView()
  123. configureScrollView()
  124. addGestureRecognizer(doubleTapGestureRecognizer)
  125. updateTransform()
  126. }
  127. private func configureScrollView() {
  128. isMultipleTouchEnabled = true
  129. showsHorizontalScrollIndicator = false
  130. showsVerticalScrollIndicator = false
  131. contentSize = imageView.bounds.size
  132. canCancelContentTouches = false
  133. zoomLevels = ZoomScale.default
  134. delegate = self
  135. bouncesZoom = false
  136. }
  137. private func resetZoom() {
  138. setZoomScale(1.0, animated: false)
  139. imageView.transform = CGAffineTransform.identity
  140. contentSize = imageView.frame.size
  141. contentOffset = .zero
  142. }
  143. private func setupIndicatorView() {
  144. addSubview(indicatorContainer)
  145. indicatorContainer.translatesAutoresizingMaskIntoConstraints = false
  146. NSLayoutConstraint.activate([
  147. indicatorContainer.widthAnchor.constraint(equalToConstant: Constants.indicatorViewSize),
  148. indicatorContainer.heightAnchor.constraint(equalToConstant: Constants.indicatorViewSize),
  149. indicatorContainer.centerXAnchor.constraint(equalTo: centerXAnchor),
  150. indicatorContainer.centerYAnchor.constraint(equalTo: centerYAnchor)
  151. ])
  152. indicatorContainer.addSubview(indicator)
  153. indicator.translatesAutoresizingMaskIntoConstraints = false
  154. NSLayoutConstraint.activate([
  155. indicator.leadingAnchor.constraint(equalTo: indicatorContainer.leadingAnchor),
  156. indicator.trailingAnchor.constraint(equalTo: indicatorContainer.trailingAnchor),
  157. indicator.topAnchor.constraint(equalTo: indicatorContainer.topAnchor),
  158. indicator.bottomAnchor.constraint(equalTo: indicatorContainer.bottomAnchor)
  159. ])
  160. indicatorContainer.setNeedsLayout()
  161. indicatorContainer.layoutIfNeeded()
  162. indicatorContainer.isHidden = true
  163. }
  164. internal func updateTransform() {
  165. NCViewerImageContentView.contentTransformer(self, position)
  166. }
  167. internal func handleChangeInViewSize(to size: CGSize) {
  168. let oldScale = zoomScale
  169. zoomScale = 1.0
  170. imageView.frame = CGRect(origin: .zero, size: size)
  171. updateImageView()
  172. updateTransform()
  173. setZoomScale(oldScale, animated: false)
  174. contentSize = imageView.frame.size
  175. }
  176. func zoomScaleOne() {
  177. if zoomScale == 1 { return }
  178. let width = bounds.size.width
  179. let height = bounds.size.height
  180. let zoomRect = CGRect(
  181. x: bounds.size.width/2 - width * 0.5,
  182. y: bounds.size.height/2 - height * 0.5,
  183. width: width,
  184. height: height
  185. )
  186. zoom(to: zoomRect, animated: true)
  187. }
  188. @objc private func didDoubleTap(_ recognizer: UITapGestureRecognizer) {
  189. let locationInImage = recognizer.location(in: imageView)
  190. let isImageCoveringScreen = imageView.frame.size.width > bounds.size.width &&
  191. imageView.frame.size.height > bounds.size.height
  192. let zoomTo = (isImageCoveringScreen || zoomScale == maximumZoomScale) ? minimumZoomScale : maximumZoomScale
  193. guard zoomTo != zoomScale else {
  194. return
  195. }
  196. let width = bounds.size.width / zoomTo
  197. let height = bounds.size.height / zoomTo
  198. let zoomRect = CGRect(
  199. x: locationInImage.x - width * 0.5,
  200. y: locationInImage.y - height * 0.5,
  201. width: width,
  202. height: height
  203. )
  204. zoom(to: zoomRect, animated: true)
  205. }
  206. }
  207. // MARK: - UIScrollViewDelegate
  208. extension NCViewerImageContentView: UIScrollViewDelegate {
  209. public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  210. let shouldAllowZoom = (image != nil && position == 0.0)
  211. return shouldAllowZoom ? imageView : nil
  212. }
  213. public func scrollViewDidZoom(_ scrollView: UIScrollView) {
  214. centerImageView()
  215. }
  216. private func centerImageView() {
  217. var imageViewFrame = imageView.frame
  218. if imageViewFrame.size.width < bounds.size.width {
  219. imageViewFrame.origin.x = (bounds.size.width - imageViewFrame.size.width) / 2.0
  220. } else {
  221. imageViewFrame.origin.x = 0.0
  222. }
  223. if imageViewFrame.size.height < bounds.size.height {
  224. imageViewFrame.origin.y = (bounds.size.height - imageViewFrame.size.height) / 2.0
  225. } else {
  226. imageViewFrame.origin.y = 0.0
  227. }
  228. imageView.frame = imageViewFrame
  229. }
  230. private func updateImageView() {
  231. imageView.image = image
  232. if let contentImage = image {
  233. let imageViewSize = bounds.size
  234. let imageSize = contentImage.size
  235. var targetImageSize = imageViewSize
  236. if imageSize.width / imageSize.height > imageViewSize.width / imageViewSize.height {
  237. targetImageSize.height = imageViewSize.width / imageSize.width * imageSize.height
  238. } else {
  239. targetImageSize.width = imageViewSize.height / imageSize.height * imageSize.width
  240. }
  241. imageView.frame = CGRect(origin: .zero, size: targetImageSize)
  242. }
  243. centerImageView()
  244. }
  245. }