-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMFSliderControl.m
124 lines (98 loc) · 4.82 KB
/
MFSliderControl.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
//
// MFSliderControl.m
// Piqueturs
//
// Created by Mark Flowers on 6/29/13.
// Copyright (c) 2013 Piqueturs LLC. All rights reserved.
//
#import "MFSliderControl.h"
@interface MFSliderControl()
@property (nonatomic, strong) UIImageView *thumbImageView;
@property (nonatomic) BOOL moveSlider;
@property (nonatomic) BOOL callDelegate;
@end
@implementation MFSliderControl
- (id)initWithFrame:(CGRect)frame thumbImage:(UIImage*) thumbeImage range:(NSRange) range stepSize:(float) stepSize {
self = [super initWithFrame:frame];
if (self) {
self.thumbImage = thumbeImage;
self.range = range;
self.step = stepSize;
self.backgroundColor = [UIColor clearColor];
self.trackColor = [UIColor whiteColor];
_thumbImageView = [[UIImageView alloc] initWithImage:self.thumbImage];
CGRect frame = CGRectMake(15.0f - (self.thumbImage.size.width / 2.0), (self.frame.size.height / 2) - (self.thumbImage.size.height / 2.0f) - 10.0f, fmaxf(self.thumbImage.size.width, 40.0f), fmaxf(self.thumbImage.size.height, 40.0f));
_thumbImageView.contentMode = UIViewContentModeCenter;
_thumbImageView.frame = frame;
[self addSubview:_thumbImageView];
UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(slid:)];
[panner cancelsTouchesInView];
[self addGestureRecognizer:panner];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, self.trackColor.CGColor);
CGContextSetLineWidth(context, 4.0);
CGContextAddEllipseInRect(context, CGRectMake(13.0, (rect.size.height / 2.0) - 2.0, 4.0, 4.0));
CGContextSetFillColorWithColor(context, self.trackColor.CGColor);
CGContextStrokePath(context);
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 15.0, rect.size.height / 2.0);
CGContextAddLineToPoint(context, rect.size.width - 15.0, rect.size.height / 2.0);
CGContextStrokePath(context);
int numberOfPoints = floorf(((self.range.location + self.range.length) - self.range.location) / self.step);
float stepDistance = (rect.size.width - 30.0f) / numberOfPoints;
for (int i = 1; i <= numberOfPoints; i++) {
CGContextSetLineWidth(context, 4.0);
CGContextAddEllipseInRect(context, CGRectMake(13.0 + (i * stepDistance), (rect.size.height / 2.0) - 2.0, 4.0, 4.0));
CGContextSetFillColorWithColor(context, self.trackColor.CGColor);
CGContextStrokePath(context);
}
}
- (void) slid:(UIPanGestureRecognizer*) panner {
if(panner.state == UIGestureRecognizerStateBegan) {
if(CGRectContainsPoint(_thumbImageView.frame, [panner locationInView:self])) {
_moveSlider = YES;
} else {
_moveSlider = NO;
}
} else if(panner.state == UIGestureRecognizerStateEnded) {
_moveSlider = NO;
int numberOfPoints = floorf(((self.range.location + self.range.length) - self.range.location) / self.step);
float stepDistance = (self.frame.size.width - 30.0f) / numberOfPoints;
float lowerStep = floorf(self.thumbImageView.frame.origin.x / stepDistance) * self.step;
float delta = (self.thumbImageView.frame.origin.x /stepDistance) - lowerStep;
self.callDelegate = YES;
if(delta >= self.step / 2) {
self.value = self.range.location + lowerStep + self.step;
} else {
self.value = self.range.location + lowerStep;
}
} else {
if(_moveSlider) {
float xOffset = [panner locationInView:self].x;
xOffset = fmaxf(-5.0f, xOffset);
xOffset = fminf(self.frame.size.width - 35.0f, xOffset);
_thumbImageView.frame = CGRectMake(xOffset, _thumbImageView.frame.origin.y, _thumbImageView.frame.size.width, _thumbImageView.frame.size.height);
}
}
}
- (void) setValue:(float)value {
_value = value;
int numberOfPoints = floorf(((self.range.location + self.range.length) - self.range.location) / self.step);
float stepDistance = (self.frame.size.width - 30.0f) / numberOfPoints;
float xOffset = (value - self.range.location) * stepDistance - (self.thumbImage.size.width / 4) + 1.0f;
[UIView animateWithDuration:0.1 animations:^{
self.thumbImageView.frame = CGRectMake(xOffset, self.thumbImageView.frame.origin.y, self.thumbImageView.frame.size.width, self.thumbImageView.frame.size.height);
} completion:^(BOOL finished) {
if (self.callDelegate && [self.delegate respondsToSelector:@selector(slider:valueChanged:)]) {
[self.delegate slider:self valueChanged:self.value];
}
self.callDelegate = NO;
}];
}
@end