forked from romaHerman/timeLineiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeLineViewControl.m
More file actions
373 lines (317 loc) · 14.2 KB
/
Copy pathTimeLineViewControl.m
File metadata and controls
373 lines (317 loc) · 14.2 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//
// TimiLineViewControl.m
// Klubok
//
// Created by Roma on 8/25/14.
// Copyright (c) 2014 908 Inc. All rights reserved.
//
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "TimeLineViewControl.h"
#import "Masonry.h"
#import "MASViewAttribute.h"
#import <QuartzCore/QuartzCore.h>
const float BETTWEEN_LABEL_OFFSET = 20;
const float LINE_WIDTH = 2.0;
const float CIRCLE_RADIUS = 3.0;
const float INITIAL_PROGRESS_CONTAINER_WIDTH = 20.0;
const float PROGRESS_VIEW_CONTAINER_LEFT = 51.0;
const float VIEW_WIDTH = 225.0;
@interface TimeLineViewControl () {
BOOL didStopAnimation;
NSMutableArray *layers;
NSMutableArray *circleLayers;
int layerCounter;
int circleCounter;
CGFloat timeOffset;
CGFloat leftWidth;
CGFloat rightWidth;
CGFloat viewWidth;
}
@property(nonatomic, strong) UIView *progressViewContainer;
@property(nonatomic, strong) UIView *timeViewContainer;
@property(nonatomic, strong) UIView *progressDescriptionViewContainer;
@property(nonatomic, strong) NSMutableArray *labelDscriptionsArray;
@property(nonatomic, strong) NSMutableArray *sizes;
@end
@implementation TimeLineViewControl
@synthesize viewheight = viewheight;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (NSMutableArray *)labelDscriptionsArray {
if (!_labelDscriptionsArray) {
_labelDscriptionsArray = [[NSMutableArray alloc] init];
}
return _labelDscriptionsArray;
}
- (NSMutableArray *)sizes {
if (!_sizes) {
_sizes = [[NSMutableArray alloc] init];
}
return _sizes;
}
- (id)initWithTimeArray:(NSArray *)time andTimeDescriptionArray:(NSArray *)timeDescriptions andCurrentStatus:(int)status andFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
viewheight = 75.0;
leftWidth = frame.size.width - (PROGRESS_VIEW_CONTAINER_LEFT + INITIAL_PROGRESS_CONTAINER_WIDTH + CIRCLE_RADIUS * 2);
self.progressViewContainer = [[UIView alloc] init ];
self.timeViewContainer = [[UIView alloc] init ];
self.progressDescriptionViewContainer = [[UIView alloc] init];
[self addSubview:self.progressViewContainer];
[self addSubview:self.timeViewContainer];
[self addSubview:self.progressDescriptionViewContainer];
//uncomment to see color view's borders
/*
self.timeViewContainer.layer.borderColor = UIColor.blackColor.CGColor;
self.timeViewContainer.layer.borderWidth = 1;
self.progressDescriptionViewContainer.layer.borderColor = UIColor.redColor.CGColor;
self.progressDescriptionViewContainer.layer.borderWidth = 1;
self.progressViewContainer.layer.borderColor = UIColor.greenColor.CGColor;
self.progressViewContainer.layer.borderWidth = 1;
*/
[self addTimeDescriptionLabels:timeDescriptions andTime:time currentStatus:status];
[self setNeedsUpdateConstraints];
[self addProgressBasedOnLabels:self.labelDscriptionsArray currentStatus:status];
[self addTimeLabels:time currentStatus:status];
}
return self;
}
- (void)addTimeLabels:(NSArray *)time currentStatus:(int)currentStatus {
CGFloat betweenLabelOffset = 0;
CGFloat totlaHeight = 6;
int i = 0;
for (NSString *timeDescription in time) {
UILabel *label = [[UILabel alloc] init];
[label setText:timeDescription];
label.numberOfLines = 2;
label.textColor = i < currentStatus ? [UIColor blackColor] : [UIColor grayColor];
label.textAlignment = NSTextAlignmentRight;
label.lineBreakMode = NSLineBreakByWordWrapping;
[label setFont:[UIFont fontWithName:@"HelveticaNeue" size:12.0]];
[self.timeViewContainer addSubview:label];
UILabel *descrLabel = self.labelDscriptionsArray[i];
[label makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(16));
make.left.equalTo(_timeViewContainer);
make.width.equalTo(_timeViewContainer);
make.top.equalTo(descrLabel.top);//.with.offset(betweenLabelOffset + 1);
}];
CGSize fittingSize = [label systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
betweenLabelOffset = BETTWEEN_LABEL_OFFSET;
totlaHeight += (fittingSize.height + betweenLabelOffset);
[self.labelDscriptionsArray addObject:label];
i++;
}
viewheight = totlaHeight;
// tell constraints they need updating
[self setNeedsUpdateConstraints];
// update constraints now
[self updateConstraintsIfNeeded];
}
- (void)addTimeDescriptionLabels:(NSArray *)timeDescriptions andTime:(NSArray *)time currentStatus:(int)currentStatus {
CGFloat betweenLabelOffset = 0;
CGFloat totlaHeight = 6;
CGSize fittingSizeLabel;
UILabel *lastLabel = [[UILabel alloc] initWithFrame:_progressDescriptionViewContainer.frame];
[_progressDescriptionViewContainer addSubview:lastLabel];
int i = 0;
for (NSString *timeDescription in timeDescriptions) {
UILabel *label = [[UILabel alloc] init];
[label setText:timeDescription];
label.numberOfLines = 0;
label.textColor = i < currentStatus ? [UIColor blackColor] : [UIColor grayColor];
label.textAlignment = NSTextAlignmentLeft;
label.lineBreakMode = NSLineBreakByWordWrapping;
[label setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]];
[self.progressDescriptionViewContainer addSubview:label];
[label makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_progressDescriptionViewContainer).with.offset(7);
make.width.equalTo(leftWidth);
make.top.equalTo(lastLabel.bottom).with.offset(betweenLabelOffset);
make.height.greaterThanOrEqualTo(@(16));
}];
[label setPreferredMaxLayoutWidth:leftWidth];
[label sizeToFit];
CGSize fittingSizeLabel = [label systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
betweenLabelOffset = BETTWEEN_LABEL_OFFSET;
totlaHeight += (fittingSizeLabel.height + betweenLabelOffset);
lastLabel = label;
[self.labelDscriptionsArray addObject:label];
i++;
}
viewheight = fittingSizeLabel.height;
// tell constraints they need updating
[self setNeedsUpdateConstraints];
// update constraints now
[self updateConstraintsIfNeeded];
}
- (void)addProgressBasedOnLabels:(NSArray *)labels currentStatus:(int)currentStatus {
int i = 0;
CGFloat betweenLineOffset = 0;
CGFloat totlaHeight = 8;
CGPoint lastpoint;
CGFloat yCenter;
UIColor *strokeColor;
CGPoint toPoint;
CGPoint fromPoint;
circleLayers = [[NSMutableArray alloc] init];
layers = [[NSMutableArray alloc] init];
for (UILabel *label in labels) {
//configure circle
CGSize fittingSize = [label systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
strokeColor = i < currentStatus ? [UIColor orangeColor] : [UIColor lightGrayColor];
yCenter = (totlaHeight /*+ fittingSize.height/2*/);
UIBezierPath *circle = [UIBezierPath bezierPath];
[self configureBezierCircle:circle withCenterY:yCenter];
CAShapeLayer *circleLayer = [self getLayerWithCircle:circle andStrokeColor:strokeColor];
[circleLayers addObject:circleLayer];
//add static background gray circle
CAShapeLayer *grayStaticCircleLayer = [self getLayerWithCircle:circle andStrokeColor:[UIColor lightGrayColor]];
[self.progressViewContainer.layer addSublayer:grayStaticCircleLayer];
//configure line
if (i > 0) {
fromPoint = lastpoint;
toPoint = CGPointMake(lastpoint.x, yCenter - CIRCLE_RADIUS);
lastpoint = CGPointMake(lastpoint.x, yCenter + CIRCLE_RADIUS);
UIBezierPath *line = [self getLineWithStartPoint:fromPoint endPoint:toPoint];
CAShapeLayer *lineLayer = [self getLayerWithLine:line andStrokeColor:strokeColor];
[layers addObject:lineLayer];
//add static background gray line
CAShapeLayer *grayStaticLineLayer = [self getLayerWithLine:line andStrokeColor:[UIColor lightGrayColor]];
[self.progressViewContainer.layer addSublayer:grayStaticLineLayer];
} else {
lastpoint = CGPointMake(self.progressViewContainer.center.x + CIRCLE_RADIUS + INITIAL_PROGRESS_CONTAINER_WIDTH / 2, yCenter + CIRCLE_RADIUS);
}
betweenLineOffset = BETTWEEN_LABEL_OFFSET;
totlaHeight += (fittingSize.height + betweenLineOffset);
i++;
}
[self startAnimatingLayers:circleLayers forStatus:currentStatus];
}
- (CAShapeLayer *)getLayerWithLine:(UIBezierPath *)line andStrokeColor:(UIColor *)strokeColor {
CAShapeLayer *lineLayer = [CAShapeLayer layer];
lineLayer.path = line.CGPath;
lineLayer.strokeColor = strokeColor.CGColor;
lineLayer.fillColor = nil;
lineLayer.lineWidth = LINE_WIDTH;
return lineLayer;
}
- (UIBezierPath *)getLineWithStartPoint:(CGPoint)start endPoint:(CGPoint)end {
UIBezierPath *line = [UIBezierPath bezierPath];
[line moveToPoint:start];
[line addLineToPoint:end];
return line;
}
- (CAShapeLayer *)getLayerWithCircle:(UIBezierPath *)circle andStrokeColor:(UIColor *)strokeColor {
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.frame = self.progressViewContainer.bounds;
circleLayer.path = circle.CGPath;
circleLayer.strokeColor = strokeColor.CGColor;
circleLayer.fillColor = nil;
circleLayer.lineWidth = LINE_WIDTH;
circleLayer.lineJoin = kCALineJoinBevel;
return circleLayer;
}
- (void)configureBezierCircle:(UIBezierPath *)circle withCenterY:(CGFloat)centerY {
[circle addArcWithCenter:CGPointMake(self.progressViewContainer.center.x + CIRCLE_RADIUS + INITIAL_PROGRESS_CONTAINER_WIDTH / 2, centerY)
radius:CIRCLE_RADIUS
startAngle:M_PI_2
endAngle:-M_PI_2
clockwise:YES];
[circle addArcWithCenter:CGPointMake(self.progressViewContainer.center.x + CIRCLE_RADIUS + + INITIAL_PROGRESS_CONTAINER_WIDTH / 2, centerY)
radius:CIRCLE_RADIUS
startAngle:-M_PI_2
endAngle:M_PI_2
clockwise:YES];
}
- (void)startAnimatingLayers:(NSArray *)layersToAnimate forStatus:(int)currentStatus {
float circleTimeOffset = 1;
circleCounter = 0;
int i = 1;
//NSLog(@"CUR ST = %i layer to anim = %lu", currentStatus, (unsigned long)layersToAnimate.count);
if (currentStatus == layersToAnimate.count) {
//add without animation
for (CAShapeLayer *cilrclLayer in layersToAnimate) {
[self.progressViewContainer.layer addSublayer:cilrclLayer];
}
for (CAShapeLayer *lineLayer in layers) {
[self.progressViewContainer.layer addSublayer:lineLayer];
}
} else {
//add with animation
for (CAShapeLayer *cilrclLayer in layersToAnimate) {
[self.progressViewContainer.layer addSublayer:cilrclLayer];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = 0.2;
animation.beginTime = [cilrclLayer convertTime:CACurrentMediaTime() fromLayer:nil] + circleTimeOffset;
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.fillMode = kCAFillModeForwards;
animation.delegate = self;
circleTimeOffset += .4;
[cilrclLayer setHidden:YES];
[cilrclLayer addAnimation:animation forKey:@"strokeCircleAnimation"];
if (i == currentStatus && i != [layersToAnimate count]) {
CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
strokeAnim.fromValue = (id) [UIColor orangeColor].CGColor;
strokeAnim.toValue = (id) [UIColor clearColor].CGColor;
strokeAnim.duration = 1.0;
strokeAnim.repeatCount = HUGE_VAL;
strokeAnim.autoreverses = NO;
[cilrclLayer addAnimation:strokeAnim forKey:@"animateStrokeColor"];
}
i++;
}
}
}
- (void)animationDidStart:(CAAnimation *)anim {
if (circleCounter < circleLayers.count) {
if (anim == [circleLayers[circleCounter] animationForKey:@"strokeCircleAnimation"]) {
[circleLayers[circleCounter] setHidden:NO];
circleCounter++;
}
}
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (layerCounter >= layers.count) {
return;
}
CAShapeLayer *lineLayer = layers[layerCounter];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = 0.200;
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.fillMode = kCAFillModeForwards;
[self.progressViewContainer.layer addSublayer:lineLayer];
[lineLayer addAnimation:animation forKey:@"strokeEndAnimation"];
layerCounter++;
}
- (void)updateConstraints {
[self.progressViewContainer updateConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@(CIRCLE_RADIUS + INITIAL_PROGRESS_CONTAINER_WIDTH));
make.height.equalTo(viewheight);
make.top.equalTo(self);
make.left.equalTo(@(PROGRESS_VIEW_CONTAINER_LEFT));
}];
[self.timeViewContainer updateConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self);
make.right.equalTo(_progressViewContainer.left);
make.top.equalTo(self);
make.height.equalTo(viewheight);
}];
[self.progressDescriptionViewContainer updateConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self);
make.left.equalTo(_progressViewContainer.right).with.offset(0);
make.top.equalTo(self);
make.height.equalTo(viewheight);
}];
[super updateConstraints];
}
@end