forked from ahmetws/DashedSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashedSlider.swift
202 lines (160 loc) · 6.82 KB
/
DashedSlider.swift
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
//
// DashedSlider.swift
//
//
// Created by Ahmet Yalcinkaya on 1/31/16.
// Copyright © 2016 ayalcinkaya
//
import UIKit
import CoreGraphics
public class DashedSlider: UISlider {
let DEFAULT_MARKER_COUNT:Int = 25
let DEFAULT_MARK_WIDTH:CGFloat = 4.0
let DEFAULT_TOP_MARGIN:CGFloat = 3.0
public var selectedBarColor:UIColor! {
didSet {
self.setNeedsLayout()
}
}
public var unselectedBarColor:UIColor! {
didSet {
self.setNeedsLayout()
}
}
public var markColor:UIColor! {
didSet {
self.setNeedsLayout()
}
}
public var handlerColor:UIColor? {
didSet {
self.setNeedsLayout()
}
}
// number of markers to draw
// 1 to 100
public var markerCount:Int! {
didSet {
self.setNeedsLayout()
}
}
public var markWidth:CGFloat! {
didSet {
self.setNeedsLayout()
}
}
public var topMargin:CGFloat! {
didSet {
self.setNeedsLayout()
}
}
public var handlerWidth:CGFloat? {
didSet {
self.setNeedsLayout()
}
}
override public init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
func configure(){
if selectedBarColor == nil {
selectedBarColor = UIColor.redColor()
}
if unselectedBarColor == nil {
unselectedBarColor = UIColor.lightGrayColor()
}
if markColor == nil {
markColor = UIColor.whiteColor()
}
if markWidth == nil {
markWidth = DEFAULT_MARK_WIDTH
}
if topMargin == nil {
topMargin = DEFAULT_TOP_MARGIN
}
if markerCount == nil {
markerCount = DEFAULT_MARKER_COUNT
}
}
override public func drawRect(rect: CGRect) {
super.drawRect(rect)
// Create an innerRect to paint the lines
let innerRect:CGRect = CGRectInset(rect, 1.0, 1.0)
UIGraphicsBeginImageContextWithOptions(innerRect.size, false, 0)
let context:CGContextRef = UIGraphicsGetCurrentContext()!
// Selected side
CGContextSetFillColorWithColor(context, self.selectedBarColor.CGColor);
CGContextFillRect(context, CGRectMake(0, topMargin, innerRect.size.width, innerRect.size.height - topMargin*2.0))
let selectedSide:UIImage = UIGraphicsGetImageFromCurrentImageContext().resizableImageWithCapInsets(UIEdgeInsetsZero)
// Unselected side
CGContextSetFillColorWithColor(context, self.unselectedBarColor.CGColor);
CGContextFillRect(context, CGRectMake(0, topMargin, innerRect.size.width, innerRect.size.height - (topMargin * 2.0)))
let unselectedSide:UIImage = UIGraphicsGetImageFromCurrentImageContext().resizableImageWithCapInsets(UIEdgeInsetsZero)
// Set markers on selected side
selectedSide.drawAtPoint(CGPoint(x: 0, y: 0))
// marker can not be less than 1
if markerCount <= 0 {
markerCount = DEFAULT_MARKER_COUNT
}
let spaceBetweenMarkers:CGFloat = 100.0 / CGFloat(markerCount)
for var i:CGFloat = 0; i < 100 ; i = i + spaceBetweenMarkers {
CGContextSetLineWidth(context, self.markWidth);
let position:CGFloat = i * innerRect.size.width / 100.0
CGContextMoveToPoint(context, position, 0);
CGContextAddLineToPoint(context, position, CGRectGetHeight(innerRect));
CGContextSetStrokeColorWithColor(context, self.markColor.CGColor)
CGContextStrokePath(context);
}
let selectedStripSide:UIImage = UIGraphicsGetImageFromCurrentImageContext().resizableImageWithCapInsets(UIEdgeInsetsZero)
// Set markers on unselected side
unselectedSide.drawAtPoint(CGPoint(x: 0, y: 0))
for var i:CGFloat = 0; i < 100 ; i = i + spaceBetweenMarkers {
CGContextSetLineWidth(context, self.markWidth);
let position:CGFloat = i * innerRect.size.width / 100.0
CGContextMoveToPoint(context, position, 0);
CGContextAddLineToPoint(context, position, CGRectGetHeight(innerRect));
CGContextSetStrokeColorWithColor(context, self.markColor.CGColor);
CGContextStrokePath(context);
}
let unselectedStripSide:UIImage = UIGraphicsGetImageFromCurrentImageContext().resizableImageWithCapInsets(UIEdgeInsetsZero)
UIGraphicsEndImageContext();
self.setMinimumTrackImage(selectedStripSide, forState: UIControlState.Normal)
self.setMaximumTrackImage(unselectedStripSide, forState: UIControlState.Normal)
if let trackImageColor = handlerColor,
let trackImageWidth = handlerWidth {
let trackImage:UIImage = UIImage.imageWithColor(trackImageColor, cornerRadius: 0.0).imageWithMinimumSize(CGSize(width: trackImageWidth, height: CGRectGetHeight(innerRect)))
self.setThumbImage(trackImage, forState: UIControlState.Normal)
self.setThumbImage(trackImage, forState: UIControlState.Highlighted)
self.setThumbImage(trackImage, forState: UIControlState.Selected)
}
}
}
extension UIImage {
static func imageWithColor(color:UIColor,cornerRadius:CGFloat) -> UIImage {
let minEdgeSize:CGFloat = cornerRadius * 2 + 1 // edge size from corner radius
let rect:CGRect = CGRectMake(0, 0, minEdgeSize, minEdgeSize);
let roundedRect:UIBezierPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
roundedRect.lineWidth = 0;
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
roundedRect.fill()
roundedRect.stroke()
roundedRect.addClip()
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image.resizableImageWithCapInsets(UIEdgeInsets(top: cornerRadius, left: cornerRadius, bottom: cornerRadius, right: cornerRadius))
}
func imageWithMinimumSize(size:CGSize) -> UIImage {
let rect:CGRect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), false, 0.0)
self.drawInRect(rect)
let resized:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resized.resizableImageWithCapInsets(UIEdgeInsets(top: size.height/2, left: size.width/2, bottom: size.height/2, right: size.width/2))
}
}