/
RMBlurredView.m
115 lines (91 loc) · 3.37 KB
/
RMBlurredView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// RMBlurredView.m
//
// Created by Raffael Hannemann on 08.10.13.
// Copyright (c) 2013 Raffael Hannemann. All rights reserved.
//
#import "RMBlurredView.h"
#define kRMBlurredViewDefaultTintColor [NSColor colorWithCalibratedWhite:1.0 alpha:0.7]
#define kRMBlurredViewDefaultSaturationFactor 2.0
#define kRMBlurredViewDefaultBlurRadius 20.0
@implementation RMBlurredView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUp];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setUp];
}
return self;
}
- (void) setTintColor:(NSColor *)tintColor {
_tintColor = tintColor;
// Since we need a CGColor reference, store it for the drawing of the layer.
if (_tintColor) {
[self.layer setBackgroundColor:_tintColor.CGColor];
}
// Trigger a re-drawing of the layer
[self.layer setNeedsDisplay];
}
- (void) setBlurRadius:(float)blurRadius {
// Setting the blur radius requires a resetting of the filters
_blurRadius = blurRadius;
[self resetFilters];
}
- (void) setBlurRadiusNumber:(NSNumber *)blurRadiusNumber {
[self setBlurRadius:blurRadiusNumber.floatValue];
}
- (void) setSaturationFactor:(float)saturationFactor {
// Setting the saturation factor also requires a resetting of the filters
_saturationFactor = saturationFactor;
[self resetFilters];
}
- (void) setSaturationFactorNumber:(NSNumber *)saturationFactorNumber {
[self setSaturationFactor:saturationFactorNumber.floatValue];
}
- (void) setUp {
// Instantiate a new CALayer and set it as the NSView's layer (layer-hosting)
_hostedLayer = [CALayer layer];
[self setWantsLayer:YES];
[self setLayer:_hostedLayer];
// Set up the default parameters
_blurRadius = kRMBlurredViewDefaultBlurRadius;
_saturationFactor = kRMBlurredViewDefaultSaturationFactor;
[self setTintColor:kRMBlurredViewDefaultTintColor];
// It's important to set the layer to mask to its bounds, otherwise the whole parent view might get blurred
[self.layer setMasksToBounds:YES];
// To apply CIFilters on OS X 10.9, we need to set the property accordingly:
if ([self respondsToSelector:@selector(setLayerUsesCoreImageFilters:)]) {
BOOL flag = YES;
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(setLayerUsesCoreImageFilters:)]];
[inv setSelector:@selector(setLayerUsesCoreImageFilters:)];
[inv setArgument:&flag atIndex:2];
[inv invokeWithTarget:self];
}
// Set the layer to redraw itself once it's size is changed
[self.layer setNeedsDisplayOnBoundsChange:YES];
// Initially create the filter instances
[self resetFilters];
}
- (void) resetFilters {
// To get a higher color saturation, we create a ColorControls filter
_saturationFilter = [CIFilter filterWithName:@"CIColorControls"];
[_saturationFilter setDefaults];
[_saturationFilter setValue:[NSNumber numberWithFloat:_saturationFactor] forKey:@"inputSaturation"];
// Next, we create the blur filter
_blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[_blurFilter setDefaults];
[_blurFilter setValue:[NSNumber numberWithFloat:_blurRadius] forKey:@"inputRadius"];
// Now we apply the two filters as the layer's background filters
[self.layer setBackgroundFilters:@[_saturationFilter, _blurFilter]];
// ... and trigger a refresh
[self.layer setNeedsDisplay];
}
@end