-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathCircularProgressView.m
206 lines (183 loc) · 7.6 KB
/
CircularProgressView.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
//
// CircularProgressView.m
// CircularProgressView
//
// Created by nijino saki on 13-3-2.
// Copyright (c) 2013年 nijino. All rights reserved.
// QQ:20118368
// http://www.nijino.cn
#import "CircularProgressView.h"
#import <QuartzCore/QuartzCore.h>
@interface CircularProgressView ()<AVAudioPlayerDelegate>
@property (nonatomic) CADisplayLink *displayLink;
@property (nonatomic) AVAudioPlayer *player;//an AVAudioPlayer instance
@property (nonatomic) CAShapeLayer *progressLayer;
@property (nonatomic) float progress;
@property (nonatomic) CGFloat angle;//angle between two lines
@end
@implementation CircularProgressView
- (id)initWithFrame:(CGRect)frame
backColor:(UIColor *)backColor
progressColor:(UIColor *)progressColor
lineWidth:(CGFloat)lineWidth
audioURL:(NSURL *)audioURL {
self = [super initWithFrame:frame];
if (self) {
[self setUp];
_backColor = backColor;
_progressColor = progressColor;
self.lineWidth = lineWidth;
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
_audioURL = audioURL;
_player.delegate = self;
[_player prepareToPlay];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[self setUp];
}
return self;
}
- (void)setUp{
self.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[self addGestureRecognizer:tapGesture];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
}
- (void)setLineWidth:(CGFloat)lineWidth{
CAShapeLayer *backgroundLayer = [self createRingLayerWithCenter:CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2) radius:CGRectGetWidth(self.bounds) / 2 - lineWidth / 2 lineWidth:lineWidth color:self.backColor];
_lineWidth = lineWidth;
[self.layer addSublayer:backgroundLayer];
_progressLayer = [self createRingLayerWithCenter:CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2) radius:CGRectGetWidth(self.bounds) / 2 - lineWidth / 2 lineWidth:lineWidth color:self.progressColor];
_progressLayer.strokeEnd = 0;
[self.layer addSublayer:_progressLayer];
}
- (void)setAudioURL:(NSURL *)audioURL{
if (audioURL) {
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
self.player.delegate = self;
self.duration = self.player.duration;
[self.player prepareToPlay];
}
_audioURL = audioURL;
}
- (CAShapeLayer *)createRingLayerWithCenter:(CGPoint)center radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth color:(UIColor *)color {
UIBezierPath *smoothedPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius startAngle:- M_PI_2 endAngle:(M_PI + M_PI_2) clockwise:YES];
CAShapeLayer *slice = [CAShapeLayer layer];
slice.contentsScale = [[UIScreen mainScreen] scale];
slice.frame = CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2);
slice.fillColor = [UIColor clearColor].CGColor;
slice.strokeColor = color.CGColor;
slice.lineWidth = lineWidth;
slice.lineCap = kCALineJoinBevel;
slice.lineJoin = kCALineJoinBevel;
slice.path = smoothedPath.CGPath;
return slice;
}
- (void)setProgress:(float)progress{
if (progress == 0) {
self.progressLayer.hidden = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progressLayer.strokeEnd = 0;
});
}else {
self.progressLayer.hidden = NO;
self.progressLayer.strokeEnd = progress;
}
}
- (void)updateProgressCircle{
//update progress value
self.progress = (float) (self.player.currentTime / self.player.duration);
if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressViewDelegate)]) {
[self.delegate updateProgressViewWithPlayer:self.player];
}
}
- (void)play{
if (!self.player.playing) {
if (!self.displayLink) {
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgressCircle)];
self.displayLink.frameInterval = 6;
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
} else {
self.displayLink.paused = NO;
}
[self.player play];
}
}
- (void)pause{
if (self.player.playing) {
self.displayLink.paused = YES;
[self.player pause];
}
}
- (void)stop{
[self.player stop];
self.progress = 0;
self.player.currentTime = 0;
[self.displayLink invalidate];
self.displayLink = nil;
}
#pragma mark AVAudioPlayerDelegate method
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
if (flag) {
[self.displayLink invalidate];
self.displayLink = nil;
//restore progress value
self.progress = 0;
[self.delegate playerDidFinishPlaying];
}
}
- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer{
CGPoint point = [recognizer locationInView:self];
self.angle = [self angleFromStartToPoint:point];
self.player.currentTime = self.player.duration * (self.angle / (2 * M_PI));
if (!self.player.playing) {
[self play];
}
[self.delegate updatePlayOrPauseButton];
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateChanged) {
self.displayLink.paused = YES;
CGPoint point = [recognizer locationInView:self];
self.angle = [self angleFromStartToPoint:point];
self.progress = self.angle/(M_PI * 2);
if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressViewDelegate)]) {
[self.delegate updateProgressViewWithPlayer:self.player];
}
}
else if (recognizer.state == UIGestureRecognizerStateEnded) {
self.player.currentTime = self.player.duration * (self.angle / (2 * M_PI));
if (!self.player.playing) [self play];
else
self.displayLink.paused = NO;
[self.delegate updatePlayOrPauseButton];
}
}
//calculate angle between start to point
- (CGFloat)angleFromStartToPoint:(CGPoint)point{
CGFloat angle = [self angleBetweenLinesWithLine1Start:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2)
Line1End:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2 - 1)
Line2Start:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2)
Line2End:point];
if (CGRectContainsPoint(CGRectMake(0, 0, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame)), point)) {
angle = 2 * M_PI - angle;
}
return angle;
}
//calculate angle between 2 lines
- (CGFloat)angleBetweenLinesWithLine1Start:(CGPoint)line1Start
Line1End:(CGPoint)line1End
Line2Start:(CGPoint)line2Start
Line2End:(CGPoint)line2End{
CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;
return acos(((a * c) + (b * d)) / ((sqrt(a * a + b * b)) * (sqrt(c * c + d * d))));
}
@end