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.

92 lines
2.6 KiB

  1. //
  2. // VisualEffectView.swift
  3. // VisualEffectView
  4. //
  5. // Created by Lasha Efremidze on 5/26/16.
  6. // Copyright © 2016 Lasha Efremidze. All rights reserved.
  7. //
  8. import UIKit
  9. /// VisualEffectView is a dynamic background blur view.
  10. open class VisualEffectView: UIVisualEffectView {
  11. /// Returns the instance of UIBlurEffect.
  12. private let blurEffect = (NSClassFromString("_UICustomBlurEffect") as! UIBlurEffect.Type).init()
  13. /**
  14. Tint color.
  15. The default value is nil.
  16. */
  17. open var colorTint: UIColor? {
  18. get { return _value(forKey: "colorTint") as? UIColor }
  19. set { _setValue(newValue, forKey: "colorTint") }
  20. }
  21. /**
  22. Tint color alpha.
  23. The default value is 0.0.
  24. */
  25. open var colorTintAlpha: CGFloat {
  26. get { return _value(forKey: "colorTintAlpha") as! CGFloat }
  27. set { _setValue(newValue, forKey: "colorTintAlpha") }
  28. }
  29. /**
  30. Blur radius.
  31. The default value is 0.0.
  32. */
  33. open var blurRadius: CGFloat {
  34. get { return _value(forKey: "blurRadius") as! CGFloat }
  35. set { _setValue(newValue, forKey: "blurRadius") }
  36. }
  37. /**
  38. Scale factor.
  39. The scale factor determines how content in the view is mapped from the logical coordinate space (measured in points) to the device coordinate space (measured in pixels).
  40. The default value is 1.0.
  41. */
  42. open var scale: CGFloat {
  43. get { return _value(forKey: "scale") as! CGFloat }
  44. set { _setValue(newValue, forKey: "scale") }
  45. }
  46. // MARK: - Initialization
  47. public override init(effect: UIVisualEffect?) {
  48. super.init(effect: effect)
  49. commonInit()
  50. }
  51. required public init?(coder aDecoder: NSCoder) {
  52. super.init(coder: aDecoder)
  53. commonInit()
  54. }
  55. private func commonInit() {
  56. scale = 1
  57. }
  58. // MARK: - Helpers
  59. /// Returns the value for the key on the blurEffect.
  60. private func _value(forKey key: String) -> Any? {
  61. return blurEffect.value(forKeyPath: key)
  62. }
  63. /// Sets the value for the key on the blurEffect.
  64. private func _setValue(_ value: Any?, forKey key: String) {
  65. blurEffect.setValue(value, forKeyPath: key)
  66. self.effect = blurEffect
  67. }
  68. }
  69. // ["grayscaleTintLevel", "grayscaleTintAlpha", "lightenGrayscaleWithSourceOver", "colorTint", "colorTintAlpha", "colorBurnTintLevel", "colorBurnTintAlpha", "darkeningTintAlpha", "darkeningTintHue", "darkeningTintSaturation", "darkenWithSourceOver", "blurRadius", "saturationDeltaFactor", "scale", "zoom"]