NCViewerImageContentView.swift 9.4 KB

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