Placeholder.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // Placeholder.swift
  3. // Kingfisher
  4. //
  5. // Created by Tieme van Veen on 28/08/2017.
  6. //
  7. // Copyright (c) 2018 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(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. /// Represent a placeholder type which could be set while loading as well as
  32. /// loading finished without getting an image.
  33. public protocol Placeholder {
  34. /// How the placeholder should be added to a given image view.
  35. func add(to imageView: ImageView)
  36. /// How the placeholder should be removed from a given image view.
  37. func remove(from imageView: ImageView)
  38. }
  39. /// Default implementation of an image placeholder. The image will be set or
  40. /// reset directly for `image` property of the image view.
  41. extension Placeholder where Self: Image {
  42. /// How the placeholder should be added to a given image view.
  43. public func add(to imageView: ImageView) { imageView.image = self }
  44. /// How the placeholder should be removed from a given image view.
  45. public func remove(from imageView: ImageView) { imageView.image = nil }
  46. }
  47. extension Image: Placeholder {}
  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: View {
  54. /// How the placeholder should be added to a given image view.
  55. public func add(to imageView: ImageView) {
  56. imageView.addSubview(self)
  57. self.translatesAutoresizingMaskIntoConstraints = false
  58. NSLayoutConstraint.activate([
  59. NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0),
  60. NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0),
  61. NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: imageView, attribute: .height, multiplier: 1, constant: 0),
  62. NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: imageView, attribute: .width, multiplier: 1, constant: 0)
  63. ])
  64. }
  65. /// How the placeholder should be removed from a given image view.
  66. public func remove(from imageView: ImageView) {
  67. self.removeFromSuperview()
  68. }
  69. }