-
Notifications
You must be signed in to change notification settings - Fork 18
/
WebPLoadingView.m
104 lines (88 loc) · 2.68 KB
/
WebPLoadingView.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
//
// WebPLoadingView.m
// ImageButter
//
// Created by Dalton Cherry on 9/2/15.
//
#import "WebPLoadingView.h"
@interface WebPLoadingView ()
@property(nonatomic)CAShapeLayer *shapeLayer;
@end
@implementation WebPLoadingView
#define pi 3.14159265359
#define DEGREES_TO_RADIANS(degrees) ((pi * degrees)/ 180)
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.shapeLayer = (CAShapeLayer*)self.layer;
self.squareCaps = NO;
self.lineColor = [UIColor whiteColor];
self.lineWidth = 4;
self.progress = 0;
self.backgroundColor = [UIColor clearColor];
self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
self.shapeLayer.strokeEnd = 0;
self.transform = CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(270));
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.shapeLayer.path = [self buildPath:self.bounds].CGPath;
}
- (UIBezierPath*)buildPath:(CGRect)rect {
CGFloat pad = self.lineWidth/2;
CGRect frame = rect;
frame.size.width = floor(rect.size.width-(pad*2));
frame.size.height = floor(rect.size.height-(pad*2));
frame.origin.x = pad;
frame.origin.y = pad;
return [UIBezierPath bezierPathWithOvalInRect:frame];
}
+ (Class)layerClass {
return [CAShapeLayer class];
}
- (void)setSquareCaps:(BOOL)squareCaps {
_squareCaps = squareCaps;
if (squareCaps) {
self.shapeLayer.lineCap = kCALineCapSquare;
} else {
self.shapeLayer.lineCap = kCALineCapRound;
}
}
- (void)setLineColor:(UIColor *)lineColor {
_lineColor = lineColor;
self.shapeLayer.strokeColor = lineColor.CGColor;
}
- (void)setLineWidth:(CGFloat)lineWidth {
_lineWidth = lineWidth;
self.shapeLayer.lineWidth = lineWidth;
}
- (void)setProgress:(CGFloat)progress {
CGFloat pro = progress;
if (pro < 0) {
pro = 0;
} else if (pro > 1) {
pro = 1;
}
_progress = pro;
self.shapeLayer.strokeEnd = pro;
}
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated {
CGFloat pro = progress;
if (pro < 0) {
pro = 0;
} else if (pro > 1) {
pro = 1;
}
[self.shapeLayer removeAnimationForKey:@"strokeEnd"];
[CATransaction begin];
CABasicAnimation* animate = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animate.fromValue = @(self.shapeLayer.strokeEnd);
animate.toValue = @(pro);
animate.duration = 0.5;
animate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.shapeLayer addAnimation:animate forKey:@"strokeEnd"];
self.progress = pro;
[CATransaction commit];
}
@end