Refactor BlurView to use modern Objective-C syntax and paradigms

This moves to modern Objective-C practices.

Synthesized properties now use property declarations in a private
category.

UIVisualEffectViews will now only be created once, rather than on each
change of blurType.

BlurAmount uses a class property to track the currently set amount.
This commit is contained in:
Eli Perkins 2017-04-10 11:18:39 -04:00
parent 9629416b4b
commit 71b7e59a62
4 changed files with 52 additions and 29 deletions

View File

@ -1,7 +1,9 @@
#import <UIKit/UIKit.h>
@interface BlurAmount : UIBlurEffect
+ (id)updateBlurAmount:(NSNumber*)blurAmount;
@property (nonatomic, copy) NSNumber *blurAmount;
+ (id)updateBlurAmount:(NSNumber*)blurAmount;
@end

View File

@ -2,13 +2,16 @@
#import <objc/runtime.h>
@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;
}

View File

@ -1,35 +1,43 @@
#import <UIKit/UIKit.h>
#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

View File

@ -2,4 +2,4 @@
@interface BlurViewManager : RCTViewManager
@end
@end