UIView+Blur.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. #import <objc/runtime.h>
  3. #import "UIView+Blur.h"
  4. NSString const *blurBackgroundKey = @"blurBackgroundKey";
  5. NSString const *blurTintColorKey = @"blurTintColorKey";
  6. NSString const *blurTintColorIntensityKey = @"blurTintColorIntensityKey";
  7. NSString const *blurTintColorLayerKey = @"blurTintColorLayerKey";
  8. NSString const *blurStyleKey = @"blurStyleKey";
  9. @implementation UIView (Blur)
  10. @dynamic blurBackground;
  11. @dynamic blurTintColor;
  12. @dynamic blurTintColorIntensity;
  13. @dynamic isBlurred;
  14. @dynamic blurStyle;
  15. #pragma mark - category methods
  16. -(void)enableBlur:(BOOL) enable
  17. {
  18. if(enable) {
  19. UIToolbar* view = (UIToolbar*)self.blurBackground;
  20. if(!view) {
  21. // use UIToolbar
  22. view = [[UIToolbar alloc] initWithFrame:self.bounds];
  23. objc_setAssociatedObject(self, &blurBackgroundKey, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  24. }
  25. view.clipsToBounds = YES;
  26. view.translucent = YES;
  27. // add the toolbar layer as sublayer
  28. [self.layer insertSublayer:view.layer atIndex:0];
  29. // view.barTintColor = [self.blurTintColor colorWithAlphaComponent:0.4f];
  30. } else {
  31. if(self.blurBackground) {
  32. [self.blurBackground.layer removeFromSuperlayer];
  33. }
  34. }
  35. }
  36. #pragma mark - getters/setters
  37. -(UIColor*) blurTintColor
  38. {
  39. return objc_getAssociatedObject(self, &blurTintColorKey);
  40. }
  41. -(void) setBlurTintColor:(UIColor *)blurTintColor
  42. {
  43. objc_setAssociatedObject(self, &blurTintColorKey, blurTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  44. if(self.blurBackground) {
  45. UIToolbar *toolbar = ((UIToolbar*)self.blurBackground);
  46. CALayer *colorLayer = objc_getAssociatedObject(self, &blurTintColorLayerKey);
  47. if(colorLayer==nil) {
  48. colorLayer = [CALayer layer];
  49. } else {
  50. [colorLayer removeFromSuperlayer];
  51. }
  52. if(self.blurStyle == UIViewBlurDarkStyle) {
  53. toolbar.barStyle = UIBarStyleBlackTranslucent;
  54. } else {
  55. toolbar.barStyle = UIBarStyleDefault;
  56. }
  57. colorLayer.frame = toolbar.frame;
  58. colorLayer.opacity = 0.5f*self.blurTintColorIntensity;
  59. colorLayer.opaque = NO;
  60. [toolbar.layer insertSublayer:colorLayer atIndex:1];
  61. colorLayer.backgroundColor = blurTintColor.CGColor;
  62. objc_setAssociatedObject(self, &blurTintColorLayerKey, colorLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  63. }
  64. }
  65. -(UIView*)blurBackground
  66. {
  67. return objc_getAssociatedObject(self, &blurBackgroundKey);
  68. }
  69. -(UIViewBlurStyle) blurStyle
  70. {
  71. NSNumber* style = objc_getAssociatedObject(self, &blurStyleKey);
  72. if(!style) {
  73. style = @0;
  74. }
  75. return [style intValue];
  76. }
  77. -(void)setBlurStyle:(UIViewBlurStyle)viewBlurStyle
  78. {
  79. NSNumber *style = [NSNumber numberWithInteger:viewBlurStyle];
  80. objc_setAssociatedObject(self, &blurStyleKey, style, OBJC_ASSOCIATION_RETAIN);
  81. if(self.blurBackground) {
  82. if(viewBlurStyle == UIViewBlurDarkStyle) {
  83. ((UIToolbar*)self.blurBackground).barStyle = UIBarStyleBlackTranslucent;
  84. } else {
  85. ((UIToolbar*)self.blurBackground).barStyle = UIBarStyleDefault;
  86. }
  87. }
  88. }
  89. -(void)setBlurTintColorIntensity:(CGFloat)blurTintColorIntensity
  90. {
  91. NSNumber *intensity = [NSNumber numberWithFloat:blurTintColorIntensity];
  92. objc_setAssociatedObject(self, &blurTintColorIntensityKey, intensity, OBJC_ASSOCIATION_RETAIN);
  93. if(self.blurBackground) {
  94. CALayer *colorLayer = objc_getAssociatedObject(self, &blurTintColorLayerKey);
  95. if(colorLayer) {
  96. colorLayer.opacity = 0.5f*intensity.floatValue;
  97. }
  98. }
  99. }
  100. -(CGFloat)blurTintColorIntensity
  101. {
  102. NSNumber *intensity = objc_getAssociatedObject(self, &blurTintColorIntensityKey);
  103. if(!intensity) {
  104. intensity = @0.6;
  105. }
  106. return intensity.floatValue;
  107. }
  108. @end