-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECProgressSlider.m
158 lines (120 loc) · 4.84 KB
/
ECProgressSlider.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
//
// ECProgressSlider.m
//
// Created by Evgeny Cherpak on 11/21/12.
//
//
#import "ECProgressSlider.h"
#import <QuartzCore/QuartzCore.h>
@interface ECProgressSlider()
@property (nonatomic, assign) CGFloat progressValue;
@property (nonatomic, assign) CGFloat loadValue;
@property (nonatomic, assign) BOOL scrubbing;
@end
@implementation ECProgressSlider
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setThumbImage:[UIImage imageNamed:@"SwitcherSliderThumb"] forState:UIControlStateNormal];
[self setThumbImage:[UIImage imageNamed:@"SwitcherSliderThumb"] forState:UIControlStateHighlighted];
self.layer.zPosition = NSIntegerMax;
[self monitorScrubbing];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setThumbImage:[UIImage imageNamed:@"SwitcherSliderThumb"] forState:UIControlStateNormal];
[self setThumbImage:[UIImage imageNamed:@"SwitcherSliderThumb"] forState:UIControlStateHighlighted];
// HACK: interface builder doesn't allow to change the height of the UISlider
CGRect frame = self.frame;
CGFloat yOffset = (frame.size.height - 10.0) / 2.0;
frame.origin.y += ceil(yOffset);
frame.size.height = 10.0f;
self.frame = frame;
self.layer.zPosition = NSIntegerMax;
[self monitorScrubbing];
}
- (void)monitorScrubbing
{
[self addTarget:self action:@selector(scrubingHasStarted:) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(scrubingHasFinished:) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(scrubingHasFinished:) forControlEvents:UIControlEventTouchUpOutside];
}
- (void)scrubingHasStarted:(id)sender
{
self.scrubbing = YES;
[self setNeedsDisplay];
}
- (void)scrubingHasFinished:(id)sender
{
self.scrubbing = NO;
[self setNeedsDisplay];
}
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent*)event {
CGRect bounds = self.bounds;
bounds = CGRectInset(bounds, -20.0, -20.0);
return CGRectContainsPoint(bounds, point);
}
- (BOOL) beginTrackingWithTouch:(UITouch*)touch withEvent:(UIEvent*)event {
return YES;
}
- (CGRect)trackRectForBounds:(CGRect)bounds {
CGRect result = [super trackRectForBounds:bounds];
result.size.height = 0;
return result;
}
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
{
CGRect trackRect = [self trackRectForBounds:bounds];
trackRect.size.height = bounds.size.height;
CGRect result = [super thumbRectForBounds:bounds trackRect:trackRect value:value];
return result;
}
- (void)setProgressValue:(CGFloat)progressValue
{
if ( !self.scrubbing ) {
self.value = progressValue;
}
_progressValue = progressValue;
[self setNeedsDisplay];
}
- (void)setLoadValue:(CGFloat)loadValue
{
_loadValue = loadValue;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGRect r = self.bounds;
CGFloat cornerRadius = r.size.height / 2.0;
CGMutablePathRef p = CGPathCreateMutable();
CGPathMoveToPoint(p, NULL, r.origin.x + cornerRadius, r.origin.y );
CGFloat maxX = CGRectGetMaxX( r );
CGFloat maxY = CGRectGetMaxY( r );
CGPathAddArcToPoint( p, NULL, maxX, r.origin.y, maxX, r.origin.y + cornerRadius, cornerRadius );
CGPathAddArcToPoint( p, NULL, maxX, maxY, maxX - cornerRadius, maxY, cornerRadius );
CGPathAddArcToPoint( p, NULL, r.origin.x, maxY, r.origin.x, maxY - cornerRadius, cornerRadius );
CGPathAddArcToPoint( p, NULL, r.origin.x, r.origin.y, r.origin.x + cornerRadius, r.origin.y, cornerRadius );
CGPathCloseSubpath(p);
CGContextAddPath(c, p);
CGContextClip(c);
CGContextSetFillColorWithColor(c, [UIColor colorWithWhite:0.667 alpha:0.2].CGColor);
CGContextFillRect(c, CGRectMake(r.origin.x, r.origin.y, r.size.width, r.size.height));
CGContextSetFillColorWithColor(c, [UIColor colorWithWhite:0.667 alpha:0.4].CGColor);
CGContextFillRect(c, CGRectMake(r.origin.x, r.origin.y, ceil(r.size.width * self.loadValue), r.size.height));
CGContextSetFillColorWithColor(c, [UIColor colorWithWhite:0.667 alpha:1.0].CGColor);
// when scrubbing we should show the current progress and when not,
// we should show current progress to the middle of the thumb (more accurate)
if ( self.scrubbing ) {
CGContextFillRect(c, CGRectMake(r.origin.x, r.origin.y, ceil(r.size.width * self.progressValue), r.size.height));
} else {
CGRect trackRect = [self trackRectForBounds:r];
CGRect thumbRect = [self thumbRectForBounds:r trackRect:trackRect value:self.value];
CGContextFillRect(c, CGRectMake(r.origin.x, r.origin.y, CGRectGetMidX(thumbRect), r.size.height));
}
}
@end