Placeholder.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // Placeholder.swift
  3. // Kingfisher
  4. //
  5. // Created by Tieme van Veen on 28/08/2017.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if !os(watchOS)
  27. #if canImport(AppKit) && !targetEnvironment(macCatalyst)
  28. import AppKit
  29. #endif
  30. #if canImport(UIKit)
  31. import UIKit
  32. #endif
  33. /// Represents a placeholder type which could be set while loading as well as
  34. /// loading finished without getting an image.
  35. public protocol Placeholder {
  36. /// How the placeholder should be added to a given image view.
  37. func add(to imageView: KFCrossPlatformImageView)
  38. /// How the placeholder should be removed from a given image view.
  39. func remove(from imageView: KFCrossPlatformImageView)
  40. }
  41. /// Default implementation of an image placeholder. The image will be set or
  42. /// reset directly for `image` property of the image view.
  43. extension KFCrossPlatformImage: Placeholder {
  44. /// How the placeholder should be added to a given image view.
  45. public func add(to imageView: KFCrossPlatformImageView) { imageView.image = self }
  46. /// How the placeholder should be removed from a given image view.
  47. public func remove(from imageView: KFCrossPlatformImageView) { imageView.image = nil }
  48. }
  49. /// Default implementation of an arbitrary view as placeholder. The view will be
  50. /// added as a subview when adding and be removed from its super view when removing.
  51. ///
  52. /// To use your customize View type as placeholder, simply let it conforming to
  53. /// `Placeholder` by `extension MyView: Placeholder {}`.
  54. extension Placeholder where Self: KFCrossPlatformView {
  55. /// How the placeholder should be added to a given image view.
  56. public func add(to imageView: KFCrossPlatformImageView) {
  57. imageView.addSubview(self)
  58. translatesAutoresizingMaskIntoConstraints = false
  59. centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
  60. centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
  61. heightAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
  62. widthAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
  63. }
  64. /// How the placeholder should be removed from a given image view.
  65. public func remove(from imageView: KFCrossPlatformImageView) {
  66. removeFromSuperview()
  67. }
  68. }
  69. #endif