BackgroundImageDesignable.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // Created by phimage on 25/03/2017.
  3. // Copyright © 2017 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. /// Protocol for designing background
  7. public protocol BackgroundDesignable: class {
  8. /**
  9. * The background view
  10. */
  11. var backgroundView: UIView? { get set }
  12. }
  13. /// Protocol for designing background image
  14. public protocol BackgroundImageDesignable: BackgroundDesignable {
  15. /**
  16. * The background image
  17. */
  18. var backgroundImage: UIImage? { get set }
  19. }
  20. public extension BackgroundImageDesignable {
  21. func configureBackgroundImage() {
  22. if let image = backgroundImage {
  23. if let imageView = backgroundView as? UIImageView {
  24. imageView.image = image
  25. } else {
  26. backgroundView = PrivateAnimatableImageView(image: image)
  27. }
  28. } else {
  29. if backgroundView is PrivateAnimatableImageView {
  30. backgroundView = nil
  31. }
  32. }
  33. }
  34. var backgroundImageView: UIImageView? {
  35. get {
  36. return backgroundView as? UIImageView
  37. }
  38. set {
  39. backgroundView = newValue
  40. }
  41. }
  42. }
  43. /// Private class of image view used in `BackgroundImageDesignable` only
  44. private class PrivateAnimatableImageView: AnimatableImageView {
  45. }