-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathBFRImageViewerDownloadProgressView.m
99 lines (75 loc) · 3.29 KB
/
BFRImageViewerDownloadProgressView.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
//
// BFRImageViewerDownloadProgressView.m
// BFRImageViewer
//
// Created by Jordan Morgan on 8/22/18.
// Copyright © 2018 Andrew Yates. All rights reserved.
//
#import "BFRImageViewerDownloadProgressView.h"
static const CGFloat BFR_PROGRESS_LINE_WIDTH = 3.0f;
@interface BFRImageViewerDownloadProgressView()
@property (strong, nonatomic, nonnull) CAShapeLayer *progressBackingLayer;
@property (strong, nonatomic, nonnull) CAShapeLayer *progressLayer;
@property (strong, nonatomic, nonnull) UIBezierPath *progressPath;
@property (nonatomic, readwrite) CGSize progessSize;
@end
@implementation BFRImageViewerDownloadProgressView
#pragma mark - Lazy Loads
- (CAShapeLayer *)progressBackingLayer {
if (!_progressBackingLayer) {
_progressBackingLayer = [CAShapeLayer new];
_progressBackingLayer.strokeColor = [UIColor lightTextColor].CGColor;
_progressBackingLayer.fillColor = [UIColor clearColor].CGColor;
_progressBackingLayer.strokeEnd = 1;
_progressBackingLayer.lineCap = kCALineCapRound;
_progressBackingLayer.lineWidth = BFR_PROGRESS_LINE_WIDTH;
}
return _progressBackingLayer;
}
- (CAShapeLayer *)progressLayer {
if (!_progressLayer) {
_progressLayer = [CAShapeLayer new];
_progressLayer.strokeColor = [UIColor whiteColor].CGColor;
_progressLayer.fillColor = [UIColor clearColor].CGColor;
_progressLayer.strokeEnd = 0;
_progressLayer.lineCap = kCALineCapRound;
_progressLayer.lineWidth = BFR_PROGRESS_LINE_WIDTH;
}
return _progressLayer;
}
#pragma mark - Custom setters
- (void)setProgress:(CGFloat)progress {
_progress = progress;
if (_progressLayer == nil) return;
_progressLayer.strokeEnd = progress;
}
#pragma mark - Initializers
- (instancetype)init {
self = [super init];
if (self) {
CGFloat targetHeightWidth = floorf([UIScreen mainScreen].bounds.size.width * .15f);
self.progessSize = CGSizeMake(targetHeightWidth, targetHeightWidth);
CGRect baseRect = CGRectMake(0, 0, self.progessSize.width, self.progessSize.height);
CGRect targetRect = CGRectInset(baseRect, BFR_PROGRESS_LINE_WIDTH/2, BFR_PROGRESS_LINE_WIDTH/2);
// Progress circle
CGFloat startAngle = M_PI_2 * 3.0f;;
CGFloat endAngle = startAngle + (M_PI * 2.0);
CGFloat width = CGRectGetWidth(targetRect)/2.0f;
CGFloat height = CGRectGetHeight(targetRect)/2.0f;
CGPoint centerPoint = CGPointMake(width, height);
float radius = targetRect.size.width/2;
self.progressPath = [UIBezierPath bezierPathWithArcCenter:centerPoint
radius:radius
startAngle:startAngle
endAngle:endAngle
clockwise:YES];
self.progressBackingLayer.path = self.progressPath.CGPath;
self.progressLayer.path = self.progressPath.CGPath;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = NO;
[self.layer addSublayer:self.progressBackingLayer];
[self.layer addSublayer:self.progressLayer];
}
return self;
}
@end