TintDesignable.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // Created by Jake Lin on 11/24/15.
  3. // Copyright © 2015 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. public protocol TintDesignable: class {
  7. /**
  8. Opacity in tint Color (White): from 0 to 1
  9. */
  10. var tintOpacity: CGFloat { get set }
  11. /**
  12. Opacity in shade Color (Black): from 0 to 1
  13. */
  14. var shadeOpacity: CGFloat { get set }
  15. /**
  16. tone color
  17. */
  18. var toneColor: UIColor? { get set }
  19. /**
  20. Opacity in tone color: from 0 to 1
  21. */
  22. var toneOpacity: CGFloat { get set }
  23. }
  24. public extension TintDesignable where Self: UIView {
  25. /**
  26. configureTintedColor method, should be called in layoutSubviews() method
  27. */
  28. func configureTintedColor() {
  29. if !tintOpacity.isNaN && tintOpacity >= 0 && tintOpacity <= 1 {
  30. addColorSubview(color: .white, opacity: tintOpacity)
  31. }
  32. if !shadeOpacity.isNaN && shadeOpacity >= 0 && shadeOpacity <= 1 {
  33. addColorSubview(color: .black, opacity: shadeOpacity)
  34. }
  35. if let toneColor = toneColor {
  36. if !toneOpacity.isNaN && toneOpacity >= 0 && toneOpacity <= 1 {
  37. addColorSubview(color: toneColor, opacity: toneOpacity)
  38. }
  39. }
  40. }
  41. fileprivate func addColorSubview(color: UIColor, opacity: CGFloat) {
  42. let subview = UIView(frame: bounds)
  43. subview.backgroundColor = color
  44. subview.alpha = opacity
  45. if layer.cornerRadius > 0 {
  46. subview.layer.cornerRadius = layer.cornerRadius
  47. }
  48. subview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  49. insertSubview(subview, at: 0)
  50. }
  51. }