forked from AlexandrGraschenkov/MagicPie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPieElement.m
269 lines (236 loc) · 7.47 KB
/
PieElement.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// PieElement.m
// MagicPie
//
// Created by Alexandr on 03.11.13.
// Copyright (c) 2013 Alexandr Graschenkov ( https://github.com/Sk0rpion )
//
// 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 "PieElement.h"
#import "PieLayer.h"
NSString * const pieElementChangedNotificationIdentifier = @"PieElementChangedNotificationIdentifier";
NSString * const pieElementAnimateChangesNotificationIdentifier = @"PieElementAnimateChangesNotificationIdentifier";
void magicPie_runOnMainQueue(void (^block)(void)) {
if ([NSThread isMainThread]) {
block();
} else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}
static BOOL animateChanges;
@interface PieLayer(hidden)
- (void)pieElementUpdate;
- (void)pieElementWillAnimateUpdate;
@end
@interface PieElement()
{
NSMutableArray* containsInLayers;
}
@property (nonatomic, assign) float titleAlpha;
@end
@implementation PieElement
+ (instancetype)pieElementWithValue:(float)val color:(UIColor *)color
{
PieElement* result = [self new];
result->_val = val;
result->_color = color;
return result;
}
- (id)init {
self = [super init];
if (self) {
_titleAlpha = 1.0;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
PieElement *another = [[[self class] allocWithZone:zone] init];
[another fillWithPieElement:self];
return another;
}
- (void)fillWithPieElement:(PieElement*)elem
{
_val = elem.val;
_color = elem.color;
_centrOffset = elem.centrOffset;
_showTitle = elem.showTitle;
_titleAlpha = elem.titleAlpha;
_maxRadius = elem.maxRadius;
_minRadius = elem.minRadius;
}
- (NSString*)description
{
return [NSString stringWithFormat:@"[%@: %f]", NSStringFromClass(self.class), self.val];
}
+ (void)animateChanges:(void (^)())changesBlock
{
magicPie_runOnMainQueue(^{
animateChanges = YES;
changesBlock();
animateChanges = NO;
});
}
- (NSArray*)animationValuesToPieElement:(PieElement*)anotherElement pieLayer:(PieLayer *)layer arrayCapacity:(NSUInteger)count
{
if(count == 1) return @[anotherElement];
layer = layer.presentationLayer ?: layer;
NSMutableArray* result = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count; i++) {
float v = i / (float)(count - 1);
UIColor* newColor = [self colorBetweenColor1:self.color color2:anotherElement.color value:v];
PieElement* newElem = [self copy];
newElem->_val = (anotherElement.val - self.val) * v + self.val;
newElem->_color = newColor;
newElem->_centrOffset = (anotherElement.centrOffset - self.centrOffset) * v + self.centrOffset;
newElem.titleAlpha = (anotherElement.titleAlpha - _titleAlpha) * v + _titleAlpha;
newElem.showTitle = self.showTitle;
if (anotherElement.maxRadius || _maxRadius) {
float from = _maxRadius ? _maxRadius.floatValue : layer.maxRadius;
float to = anotherElement.maxRadius ? anotherElement.maxRadius.floatValue : layer.maxRadius;
newElem->_maxRadius = @((to - from) * v + from);
}
if (anotherElement.minRadius || _minRadius) {
float from = _minRadius ? _minRadius.floatValue : layer.minRadius;
float to = anotherElement.minRadius ? anotherElement.minRadius.floatValue : layer.minRadius;
newElem->_minRadius = @((to - from) * v + from);
}
[result addObject:newElem];
}
return result;
}
- (void)addedToPieLayer:(PieLayer *)pieLayer
{
if(!containsInLayers)
containsInLayers = [NSMutableArray new];
NSValue* wraper = [NSValue valueWithNonretainedObject:pieLayer];
[containsInLayers addObject:wraper];
}
- (void)removedFromLayer:(PieLayer *)pieLayer
{
[containsInLayers removeObject:pieLayer];
}
- (void)notifyPerformForAnimation
{
for(NSValue* notRetainedVal in containsInLayers)
[((PieLayer *)notRetainedVal.nonretainedObjectValue) pieElementWillAnimateUpdate];
}
- (void)notifyUpdated
{
for(NSValue* notRetainedVal in containsInLayers)
[((PieLayer *)notRetainedVal.nonretainedObjectValue) pieElementUpdate];
}
#pragma mark - Setters
- (void)setVal:(float)val
{
if(val < 0){
#ifdef DEBUG
NSLog(@"[%@ %@]- Negative values not allowed: val=%f => 0.0", NSStringFromClass(self.class), NSStringFromSelector(_cmd), val);
#endif
val = 0.0;
}
if(val == _val)
return;
if(animateChanges){
[self notifyPerformForAnimation];
}
_val = val;
if(!animateChanges){
[self notifyUpdated];
}
}
- (void)setVal_:(float)val
{
_val = val;
}
- (void)setColor:(UIColor *)color
{
if([color isEqual:_color])
return;
if(animateChanges){
[self notifyPerformForAnimation];
}
_color = color;
if(!animateChanges){
[self notifyUpdated];
}
}
- (void)setCentrOffset:(float)centrOffset
{
if(_centrOffset == centrOffset)
return;
if(animateChanges){
[self notifyPerformForAnimation];
}
_centrOffset = centrOffset;
if(!animateChanges){
[self notifyUpdated];
}
}
- (void)setShowTitle:(BOOL)showTitle
{
if(_showTitle == showTitle)
return;
if(animateChanges){
[self notifyPerformForAnimation];
}
_showTitle = showTitle;
if(!animateChanges){
[self notifyUpdated];
}
}
- (void)setMaxRadius:(NSNumber *)maxRadius
{
if([maxRadius isEqual:_maxRadius])
return;
if(animateChanges){
[self notifyPerformForAnimation];
}
_maxRadius = maxRadius;
if(!animateChanges){
[self notifyUpdated];
}
}
- (void)setMinRadius:(NSNumber *)minRadius
{
if([minRadius isEqual:_minRadius])
return;
if(animateChanges){
[self notifyPerformForAnimation];
}
_minRadius = minRadius;
if(!animateChanges){
[self notifyUpdated];
}
}
#pragma mark - Helpers
- (UIColor*)colorBetweenColor1:(UIColor*)color1 color2:(UIColor*)color2 value:(float)val
{
val = MIN(MAX(val, 0.0), 1.0);
CGFloat red1 = 0.0, green1 = 0.0, blue1 = 0.0, alpha1 =0.0;
[color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
CGFloat red2 = 0.0, green2 = 0.0, blue2 = 0.0, alpha2 =0.0;
[color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
return [UIColor colorWithRed:(red2-red1)* val + red1
green:(green2-green1)*val + green1
blue:(blue2-blue1)*val + blue1
alpha:(alpha2-alpha1)*val + alpha1];
}
@end