NCViewerImageContentView.swift 11 KB

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