diff --git a/ios/BlurAmount.h b/ios/BlurAmount.h index 8f360f5..42aa33a 100644 --- a/ios/BlurAmount.h +++ b/ios/BlurAmount.h @@ -1,7 +1,9 @@ #import @interface BlurAmount : UIBlurEffect -+ (id)updateBlurAmount:(NSNumber*)blurAmount; @property (nonatomic, copy) NSNumber *blurAmount; + ++ (id)updateBlurAmount:(NSNumber*)blurAmount; + @end diff --git a/ios/BlurAmount.m b/ios/BlurAmount.m index 70c74f6..1d84977 100644 --- a/ios/BlurAmount.m +++ b/ios/BlurAmount.m @@ -2,13 +2,16 @@ #import @interface UIBlurEffect (Protected) + @property (nonatomic, readonly) id effectSettings; +@property (nonatomic, copy, class) NSNumber *localBlurAmount; + @end @implementation BlurAmount - NSNumber *localBlurAmount; +static NSNumber *_localBlurAmount = nil; + (instancetype)effectWithStyle:(UIBlurEffectStyle)style { @@ -21,7 +24,7 @@ - (id)effectSettings { id settings = [super effectSettings]; - [settings setValue:localBlurAmount forKey:@"blurRadius"]; + [settings setValue:BlurAmount.localBlurAmount forKey:@"blurRadius"]; return settings; } @@ -32,9 +35,19 @@ return result; } ++ (void)setLocalBlurAmount:(NSNumber *)localBlurAmount { + if (localBlurAmount != _localBlurAmount) { + _localBlurAmount = localBlurAmount; + } +} + ++ (NSNumber *)localBlurAmount { + return _localBlurAmount; +} + + (id)updateBlurAmount:(NSNumber*)blurAmount { - localBlurAmount = blurAmount; + self.localBlurAmount = blurAmount; return blurAmount; } diff --git a/ios/BlurView.m b/ios/BlurView.m index f6c80e0..e546ce3 100644 --- a/ios/BlurView.m +++ b/ios/BlurView.m @@ -1,35 +1,43 @@ +#import #import "BlurView.h" #import "BlurAmount.h" +@interface BlurView () -@implementation BlurView { - UIVisualEffectView *_visualEffectView; - UIBlurEffect * blurEffect; +@property (nonatomic, strong) UIVisualEffectView *visualEffectView; +@property (nonatomic, strong) UIBlurEffect *blurEffect; + +@end + +@implementation BlurView + +- (instancetype)initWithFrame:(CGRect)frame { + if (self = [super initWithFrame:frame]) { + self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; + self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:self.blurEffect]; + self.visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + self.visualEffectView.frame = frame; + + self.clipsToBounds = true; + + [self addSubview:self.visualEffectView]; + } + + return self; } - (void)setBlurType:(NSString *)blurType { - if (_visualEffectView) { - [_visualEffectView removeFromSuperview]; - } - - self.clipsToBounds = true; - if ([blurType isEqual: @"xlight"]) { - blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleExtraLight]; - } else if ([blurType isEqual: @"light"]) { - blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleLight]; - } else if ([blurType isEqual: @"dark"]) { - blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleDark]; - } else { - blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleDark]; - } - - dispatch_async(dispatch_get_main_queue(), ^{ - _visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; - _visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - _visualEffectView.frame = self.bounds; - [self insertSubview:_visualEffectView atIndex:0]; - }); + if ([blurType isEqual: @"xlight"]) { + self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]; + } else if ([blurType isEqual: @"light"]) { + self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; + } else if ([blurType isEqual: @"dark"]) { + self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; + } else { + self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; + } + self.visualEffectView.effect = self.blurEffect; } - (void)setBlurAmount:(NSNumber *)blurAmount diff --git a/ios/BlurViewManager.h b/ios/BlurViewManager.h index 0e9aea0..7aad0df 100644 --- a/ios/BlurViewManager.h +++ b/ios/BlurViewManager.h @@ -2,4 +2,4 @@ @interface BlurViewManager : RCTViewManager -@end \ No newline at end of file +@end