-
Notifications
You must be signed in to change notification settings - Fork 527
/
M13ProgressViewFilteredImage.m
201 lines (167 loc) · 6.48 KB
/
M13ProgressViewFilteredImage.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// M13ProgressViewFilteredImage.m
// M13ProgressView
//
/*Copyright (c) 2013 Brandon McQuilkin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "M13ProgressViewFilteredImage.h"
@interface M13ProgressViewFilteredImage ()
/**The start progress for the progress animation.*/
@property (nonatomic, assign) CGFloat animationFromValue;
/**The end progress for the progress animation.*/
@property (nonatomic, assign) CGFloat animationToValue;
/**The start time interval for the animaiton.*/
@property (nonatomic, assign) CFTimeInterval animationStartTime;
/**Link to the display to keep animations in sync.*/
@property (nonatomic, strong) CADisplayLink *displayLink;
@end
@implementation M13ProgressViewFilteredImage
#pragma mark Initalization and setup
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
//Set own background color
self.backgroundColor = [UIColor clearColor];
//Set defauts
self.animationDuration = .3;
//Set up the progress view
_progressView = [[UIImageView alloc] init];
_progressView.contentMode = UIViewContentModeScaleAspectFit;
_progressView.frame = self.bounds;
[self addSubview:_progressView];
//Layout
[self layoutSubviews];
}
#pragma mark Appearance
- (void)setProgressImage:(UIImage *)progressImage
{
_progressImage = progressImage;
[_progressView setImage:_progressImage];
[self setNeedsDisplay];
}
- (void)setFilters:(NSArray *)filters
{
_filters = filters;
[self setNeedsDisplay];
}
- (void)setFilterParameters:(NSArray *)filterParameters
{
_filterParameters = filterParameters;
}
#pragma mark Actions
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated
{
if (animated == NO) {
if (_displayLink) {
//Kill running animations
[_displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
_displayLink = nil;
}
[super setProgress:progress animated:NO];
[self setNeedsDisplay];
} else {
_animationStartTime = CACurrentMediaTime();
_animationFromValue = self.progress;
_animationToValue = progress;
if (!_displayLink) {
//Create and setup the display link
[self.displayLink removeFromRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(animateProgress:)];
[self.displayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
} /*else {
//Reuse the current display link
}*/
}
}
- (void)animateProgress:(CADisplayLink *)displayLink
{
dispatch_async(dispatch_get_main_queue(), ^{
CGFloat dt = (displayLink.timestamp - _animationStartTime) / self.animationDuration;
if (dt >= 1.0) {
//Order is important! Otherwise concurrency will cause errors, because setProgress: will detect an animation in progress and try to stop it by itself. Once over one, set to actual progress amount. Animation is over.
[self.displayLink removeFromRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
self.displayLink = nil;
[super setProgress:_animationToValue animated:NO];
[self setNeedsDisplay];
return;
}
//Set progress
[super setProgress:_animationFromValue + dt * (_animationToValue - _animationFromValue) animated:YES];
[self setNeedsDisplay];
});
}
- (void)performAction:(M13ProgressViewAction)action animated:(BOOL)animated
{
//Do Nothing
}
- (void)setIndeterminate:(BOOL)indeterminate
{
[super setIndeterminate:indeterminate];
//Do Nothing
}
#pragma mark Layout
- (void)layoutSubviews
{
[super layoutSubviews];
_progressView.frame = self.bounds;
}
- (CGSize)intrinsicContentSize
{
return CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric);
}
#pragma mark Drawing
- (void)drawRect:(CGRect)rect
{
//Create the base CIImage
CIImage *image = [CIImage imageWithCGImage:_progressImage.CGImage];
//Change the values of the CIFilters before drawing
for (int i = 0; i < _filters.count; i++) {
CIFilter *filter = _filters[i];
//For each filter
NSDictionary *parameters = _filterParameters[i];
//For each parameter
for (NSString *parameterKey in parameters.allKeys) {
//Retreive the values
NSDictionary *parameterDict = parameters[parameterKey];
CGFloat startValue = [parameterDict[kM13ProgressViewFilteredImageCIFilterStartValuesKey] floatValue];
CGFloat endValue = [parameterDict[kM13ProgressViewFilteredImageCIFilterEndValuesKey] floatValue];
//Calculate the current value
CGFloat value = startValue + ((endValue - startValue) * self.progress);
//Set the value
[filter setValue:[NSNumber numberWithFloat:value] forKey:parameterKey];
}
//Set the input image
[filter setValue:image forKey:kCIInputImageKey];
image = filter.outputImage;
}
//Set the image of the image view.
[_progressView setImage:[UIImage imageWithCIImage:image]];
[super drawRect:rect];
}
@end