NCViewerImageContentView.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. // Copyright © 2019 Marino Faggiana. All rights reserved.
  8. //
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. /// Holds the value of minimumZoomScale and maximumZoomScale of the image.
  25. public struct ZoomScale {
  26. /// Minimum zoom level, the image can be zoomed out to.
  27. public var minimumZoomScale: CGFloat
  28. /// Maximum zoom level, the image can be zoomed into.
  29. public var maximumZoomScale: CGFloat
  30. /// Default zoom scale. minimum is 1.0 and maximum is 3.0
  31. public static let `default` = ZoomScale(
  32. minimum: 1.0,
  33. maximum: 10.0
  34. )
  35. /// Identity zoom scale. Pass this to disable zoom.
  36. public static let identity = ZoomScale(
  37. minimum: 1.0,
  38. maximum: 1.0
  39. )
  40. public init(minimum: CGFloat, maximum: CGFloat) {
  41. minimumZoomScale = minimum
  42. maximumZoomScale = maximum
  43. }
  44. }
  45. public class NCViewerImageContentView: UIScrollView {
  46. // MARK: - Exposed variables
  47. internal static var interItemSpacing: CGFloat = 0.0
  48. internal var index: Int {
  49. didSet {
  50. resetZoom()
  51. }
  52. }
  53. internal static var contentTransformer: NCViewerImageContentTransformer = NCViewerImageDefaultContentTransformers.horizontalMoveInOut
  54. internal var position: CGFloat {
  55. didSet {
  56. updateTransform()
  57. }
  58. }
  59. internal var image: UIImage? {
  60. didSet {
  61. updateImageView()
  62. }
  63. }
  64. internal var isLoading: Bool = false {
  65. didSet {
  66. indicatorContainer.isHidden = !isLoading
  67. if isLoading {
  68. indicator.startAnimating()
  69. } else {
  70. indicator.stopAnimating()
  71. }
  72. }
  73. }
  74. internal var zoomLevels: ZoomScale? {
  75. didSet {
  76. zoomScale = ZoomScale.default.minimumZoomScale
  77. minimumZoomScale = zoomLevels?.minimumZoomScale ?? ZoomScale.default.minimumZoomScale
  78. maximumZoomScale = zoomLevels?.maximumZoomScale ?? ZoomScale.default.maximumZoomScale
  79. }
  80. }
  81. // MARK: - Private enumerations
  82. private enum Constants {
  83. static let indicatorViewSize: CGFloat = 60.0
  84. }
  85. // MARK: - Private variables
  86. private lazy var imageView: UIImageView = {
  87. let imageView = UIImageView()
  88. imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  89. imageView.contentMode = .scaleAspectFit
  90. imageView.clipsToBounds = true
  91. return imageView
  92. }()
  93. private lazy var indicator: UIActivityIndicatorView = {
  94. let indicatorView = UIActivityIndicatorView()
  95. indicatorView.style = .whiteLarge
  96. indicatorView.hidesWhenStopped = true
  97. return indicatorView
  98. }()
  99. private lazy var indicatorContainer: UIView = {
  100. let container = UIView()
  101. container.backgroundColor = .darkGray
  102. container.layer.cornerRadius = Constants.indicatorViewSize * 0.5
  103. container.layer.masksToBounds = true
  104. return container
  105. }()
  106. private lazy var doubleTapGestureRecognizer: UITapGestureRecognizer = { [unowned self] in
  107. let gesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap(_:)))
  108. gesture.numberOfTapsRequired = 2
  109. gesture.numberOfTouchesRequired = 1
  110. return gesture
  111. }()
  112. init(index itemIndex: Int, position: CGFloat, frame: CGRect) {
  113. self.index = itemIndex
  114. self.position = position
  115. super.init(frame: frame)
  116. initializeViewComponents()
  117. }
  118. required init?(coder aDecoder: NSCoder) {
  119. fatalError("Do nto use `init?(coder:)`")
  120. }
  121. }
  122. // MARK: - View Composition and Events
  123. extension NCViewerImageContentView {
  124. private func initializeViewComponents() {
  125. addSubview(imageView)
  126. imageView.frame = frame
  127. setupIndicatorView()
  128. configureScrollView()
  129. addGestureRecognizer(doubleTapGestureRecognizer)
  130. updateTransform()
  131. }
  132. private func configureScrollView() {
  133. isMultipleTouchEnabled = true
  134. showsHorizontalScrollIndicator = false
  135. showsVerticalScrollIndicator = false
  136. contentSize = imageView.bounds.size
  137. canCancelContentTouches = false
  138. zoomLevels = ZoomScale.default
  139. delegate = self
  140. bouncesZoom = false
  141. }
  142. private func resetZoom() {
  143. setZoomScale(1.0, animated: false)
  144. imageView.transform = CGAffineTransform.identity
  145. contentSize = imageView.frame.size
  146. contentOffset = .zero
  147. }
  148. private func setupIndicatorView() {
  149. addSubview(indicatorContainer)
  150. indicatorContainer.translatesAutoresizingMaskIntoConstraints = false
  151. NSLayoutConstraint.activate([
  152. indicatorContainer.widthAnchor.constraint(equalToConstant: Constants.indicatorViewSize),
  153. indicatorContainer.heightAnchor.constraint(equalToConstant: Constants.indicatorViewSize),
  154. indicatorContainer.centerXAnchor.constraint(equalTo: centerXAnchor),
  155. indicatorContainer.centerYAnchor.constraint(equalTo: centerYAnchor)
  156. ])
  157. indicatorContainer.addSubview(indicator)
  158. indicator.translatesAutoresizingMaskIntoConstraints = false
  159. NSLayoutConstraint.activate([
  160. indicator.leadingAnchor.constraint(equalTo: indicatorContainer.leadingAnchor),
  161. indicator.trailingAnchor.constraint(equalTo: indicatorContainer.trailingAnchor),
  162. indicator.topAnchor.constraint(equalTo: indicatorContainer.topAnchor),
  163. indicator.bottomAnchor.constraint(equalTo: indicatorContainer.bottomAnchor)
  164. ])
  165. indicatorContainer.setNeedsLayout()
  166. indicatorContainer.layoutIfNeeded()
  167. indicatorContainer.isHidden = true
  168. }
  169. internal func updateTransform() {
  170. NCViewerImageContentView.contentTransformer(self, position)
  171. }
  172. internal func handleChangeInViewSize(to size: CGSize) {
  173. let oldScale = zoomScale
  174. zoomScale = 1.0
  175. imageView.frame = CGRect(origin: .zero, size: size)
  176. updateImageView()
  177. updateTransform()
  178. setZoomScale(oldScale, animated: false)
  179. contentSize = imageView.frame.size
  180. }
  181. func zoomScaleOne() {
  182. if zoomScale == 1 { return }
  183. let width = bounds.size.width
  184. let height = bounds.size.height
  185. let zoomRect = CGRect(
  186. x: bounds.size.width/2 - width * 0.5,
  187. y: bounds.size.height/2 - height * 0.5,
  188. width: width,
  189. height: height
  190. )
  191. zoom(to: zoomRect, animated: true)
  192. }
  193. @objc private func didDoubleTap(_ recognizer: UITapGestureRecognizer) {
  194. let locationInImage = recognizer.location(in: imageView)
  195. let isImageCoveringScreen = imageView.frame.size.width > bounds.size.width &&
  196. imageView.frame.size.height > bounds.size.height
  197. let zoomTo = (isImageCoveringScreen || zoomScale == maximumZoomScale/2) ? minimumZoomScale : maximumZoomScale/2
  198. guard zoomTo != zoomScale else {
  199. return
  200. }
  201. let width = bounds.size.width / zoomTo
  202. let height = bounds.size.height / zoomTo
  203. let zoomRect = CGRect(
  204. x: locationInImage.x - width * 0.5,
  205. y: locationInImage.y - height * 0.5,
  206. width: width,
  207. height: height
  208. )
  209. zoom(to: zoomRect, animated: true)
  210. }
  211. }
  212. // MARK: - UIScrollViewDelegate
  213. extension NCViewerImageContentView: UIScrollViewDelegate {
  214. public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  215. let shouldAllowZoom = (image != nil && position == 0.0)
  216. return shouldAllowZoom ? imageView : nil
  217. }
  218. public func scrollViewDidZoom(_ scrollView: UIScrollView) {
  219. centerImageView()
  220. }
  221. private func centerImageView() {
  222. var imageViewFrame = imageView.frame
  223. if imageViewFrame.size.width < bounds.size.width {
  224. imageViewFrame.origin.x = (bounds.size.width - imageViewFrame.size.width) / 2.0
  225. } else {
  226. imageViewFrame.origin.x = 0.0
  227. }
  228. if imageViewFrame.size.height < bounds.size.height {
  229. imageViewFrame.origin.y = (bounds.size.height - imageViewFrame.size.height) / 2.0
  230. } else {
  231. imageViewFrame.origin.y = 0.0
  232. }
  233. imageView.frame = imageViewFrame
  234. }
  235. private func updateImageView() {
  236. imageView.image = image
  237. if let contentImage = image {
  238. let imageViewSize = bounds.size
  239. let imageSize = contentImage.size
  240. var targetImageSize = imageViewSize
  241. if imageSize.width / imageSize.height > imageViewSize.width / imageViewSize.height {
  242. targetImageSize.height = imageViewSize.width / imageSize.width * imageSize.height
  243. } else {
  244. targetImageSize.width = imageViewSize.height / imageSize.height * imageSize.width
  245. }
  246. imageView.frame = CGRect(origin: .zero, size: targetImageSize)
  247. }
  248. centerImageView()
  249. }
  250. }