Merge pull request #171 from eliperkins/blur-refactor

Refactor BlurView to use modern Objective-C syntax and paradigms
This commit is contained in:
Alexey 2017-04-11 09:58:20 +02:00 committed by GitHub
commit 09d44521f3
4 changed files with 52 additions and 29 deletions

View File

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

View File

@ -2,13 +2,16 @@
#import <objc/runtime.h> #import <objc/runtime.h>
@interface UIBlurEffect (Protected) @interface UIBlurEffect (Protected)
@property (nonatomic, readonly) id effectSettings; @property (nonatomic, readonly) id effectSettings;
@property (nonatomic, copy, class) NSNumber *localBlurAmount;
@end @end
@implementation BlurAmount @implementation BlurAmount
NSNumber *localBlurAmount; static NSNumber *_localBlurAmount = nil;
+ (instancetype)effectWithStyle:(UIBlurEffectStyle)style + (instancetype)effectWithStyle:(UIBlurEffectStyle)style
{ {
@ -21,7 +24,7 @@
- (id)effectSettings - (id)effectSettings
{ {
id settings = [super effectSettings]; id settings = [super effectSettings];
[settings setValue:localBlurAmount forKey:@"blurRadius"]; [settings setValue:BlurAmount.localBlurAmount forKey:@"blurRadius"];
return settings; return settings;
} }
@ -32,9 +35,19 @@
return result; return result;
} }
+ (void)setLocalBlurAmount:(NSNumber *)localBlurAmount {
if (localBlurAmount != _localBlurAmount) {
_localBlurAmount = localBlurAmount;
}
}
+ (NSNumber *)localBlurAmount {
return _localBlurAmount;
}
+ (id)updateBlurAmount:(NSNumber*)blurAmount + (id)updateBlurAmount:(NSNumber*)blurAmount
{ {
localBlurAmount = blurAmount; self.localBlurAmount = blurAmount;
return blurAmount; return blurAmount;
} }

View File

@ -1,35 +1,43 @@
#import <UIKit/UIKit.h>
#import "BlurView.h" #import "BlurView.h"
#import "BlurAmount.h" #import "BlurAmount.h"
@interface BlurView ()
@implementation BlurView { @property (nonatomic, strong) UIVisualEffectView *visualEffectView;
UIVisualEffectView *_visualEffectView; @property (nonatomic, strong) UIBlurEffect *blurEffect;
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 - (void)setBlurType:(NSString *)blurType
{ {
if (_visualEffectView) {
[_visualEffectView removeFromSuperview];
}
self.clipsToBounds = true;
if ([blurType isEqual: @"xlight"]) { if ([blurType isEqual: @"xlight"]) {
blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleExtraLight]; self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
} else if ([blurType isEqual: @"light"]) { } else if ([blurType isEqual: @"light"]) {
blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleLight]; self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
} else if ([blurType isEqual: @"dark"]) { } else if ([blurType isEqual: @"dark"]) {
blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleDark]; self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
} else { } else {
blurEffect = [BlurAmount effectWithStyle:UIBlurEffectStyleDark]; self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
} }
self.visualEffectView.effect = self.blurEffect;
dispatch_async(dispatch_get_main_queue(), ^{
_visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
_visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_visualEffectView.frame = self.bounds;
[self insertSubview:_visualEffectView atIndex:0];
});
} }
- (void)setBlurAmount:(NSNumber *)blurAmount - (void)setBlurAmount:(NSNumber *)blurAmount