Merge pull request #171 from eliperkins/blur-refactor
Refactor BlurView to use modern Objective-C syntax and paradigms
This commit is contained in:
commit
09d44521f3
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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];
|
||||
self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
|
||||
} else if ([blurType isEqual: @"light"]) {
|
||||
blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleLight];
|
||||
self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
|
||||
} else if ([blurType isEqual: @"dark"]) {
|
||||
blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleDark];
|
||||
self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
|
||||
} else {
|
||||
blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleDark];
|
||||
self.blurEffect = [UIBlurEffect 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];
|
||||
});
|
||||
self.visualEffectView.effect = self.blurEffect;
|
||||
}
|
||||
|
||||
- (void)setBlurAmount:(NSNumber *)blurAmount
|
||||
|
|
Loading…
Reference in New Issue