You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.3 KiB

  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 os(macOS)
  28. import AppKit
  29. #else
  30. import UIKit
  31. #endif
  32. /// Represents a placeholder type which could be set while loading as well as
  33. /// loading finished without getting an image.
  34. public protocol Placeholder {
  35. /// How the placeholder should be added to a given image view.
  36. func add(to imageView: KFCrossPlatformImageView)
  37. /// How the placeholder should be removed from a given image view.
  38. func remove(from imageView: KFCrossPlatformImageView)
  39. }
  40. /// Default implementation of an image placeholder. The image will be set or
  41. /// reset directly for `image` property of the image view.
  42. extension KFCrossPlatformImage: Placeholder {
  43. /// How the placeholder should be added to a given image view.
  44. public func add(to imageView: KFCrossPlatformImageView) { imageView.image = self }
  45. /// How the placeholder should be removed from a given image view.
  46. public func remove(from imageView: KFCrossPlatformImageView) { imageView.image = nil }
  47. }
  48. /// Default implementation of an arbitrary view as placeholder. The view will be
  49. /// added as a subview when adding and be removed from its super view when removing.
  50. ///
  51. /// To use your customize View type as placeholder, simply let it conforming to
  52. /// `Placeholder` by `extension MyView: Placeholder {}`.
  53. extension Placeholder where Self: KFCrossPlatformView {
  54. /// How the placeholder should be added to a given image view.
  55. public func add(to imageView: KFCrossPlatformImageView) {
  56. imageView.addSubview(self)
  57. translatesAutoresizingMaskIntoConstraints = false
  58. centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
  59. centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
  60. heightAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
  61. widthAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
  62. }
  63. /// How the placeholder should be removed from a given image view.
  64. public func remove(from imageView: KFCrossPlatformImageView) {
  65. removeFromSuperview()
  66. }
  67. }
  68. #endif