forked from blommegard/HSPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HSSlider.m
146 lines (104 loc) · 3.44 KB
/
HSSlider.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
//
// HSSlider.m
// SBSlider
//
// Created by Simon Blommegård on 2011-11-29.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "HSSlider.h"
@interface HSSlider ()
- (void)setup;
- (float)valueFromTouch:(UITouch *)touch;
@end
@implementation HSSlider
@synthesize value = _value;
@synthesize minimumValue = _minimumValue;
@synthesize maximumValue = _maximumValue;
@synthesize continuous = _continuous;
@synthesize strokeColor = _strokeColor;
@synthesize fillColor = _fillColor;
- (id)init {
if ((self = [super init]))
[self setup];
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame]))
[self setup];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder]))
[self setup];
return self;
}
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectInset(self.bounds, .5, .5)];
[self.strokeColor setStroke];
[path stroke];
CGRect barRect = CGRectInset(self.bounds, 2., 2.);
CGFloat barWidth = barRect.size.width;
float length = self.maximumValue - self.minimumValue;
float lenghtValue = self.value - self.minimumValue;
float percent = lenghtValue / length;
CGFloat width = percent*barWidth;
path = [UIBezierPath bezierPathWithRect:CGRectMake(barRect.origin.x, barRect.origin.y, width, barRect.size.height)];
[self.fillColor setFill];
[path fill];
}
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
BOOL returnValue = [super beginTrackingWithTouch:touch withEvent:event];
[self setValue:[self valueFromTouch:touch]];
if (self.continuous)
[self sendActionsForControlEvents:UIControlEventValueChanged];
return returnValue;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
BOOL returnValue = [super continueTrackingWithTouch:touch withEvent:event];
[self setValue:[self valueFromTouch:touch]];
if (self.continuous)
[self sendActionsForControlEvents:UIControlEventValueChanged];
return returnValue;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super endTrackingWithTouch:touch withEvent:event];
[self setValue:[self valueFromTouch:touch]];
[self sendActionsForControlEvents:UIControlEventValueChanged];
[self setNeedsDisplay];
}
#pragma mark - Properties
- (void)setValue:(float)value {
if (value > self.maximumValue)
value = self.maximumValue;
if (value < self.minimumValue)
value = self.minimumValue;
_value = value;
[self setNeedsDisplay];
}
- (UIColor *)strokeColor {
if (!_strokeColor)
_strokeColor = [UIColor whiteColor];
return _strokeColor;
}
- (UIColor *)fillColor {
if (!_fillColor)
_fillColor = [UIColor whiteColor];
return _fillColor;
}
#pragma mark - Private
- (void)setup {
[self setMinimumValue:0.];
[self setMaximumValue:1.];
[self setValue:.5];
[self setContinuous:YES];
[self setBackgroundColor:[UIColor clearColor]];
}
- (float)valueFromTouch:(UITouch *)touch {
// Border inset
CGFloat x = [touch locationInView:self].x-2;
CGFloat width = self.bounds.size.width-4.;
CGFloat percent = x / width;
float length = self.maximumValue - self.minimumValue;
return percent*length;
}
@end