-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathChangeScaleProgrammaticViewController.swift
170 lines (158 loc) · 7.85 KB
/
ChangeScaleProgrammaticViewController.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
//
// ChangeScaleProgrammaticViewController.swift
// Demo
//
// Created by Fabrizio Duroni on 03/05/2018.
// 2018 Fabrizio Duroni.
//
// swiftlint:disable function_body_length
import UIKit
import RangeUISlider
class ChangeScaleProgrammaticViewController: UIViewController, RangeUISliderDelegate {
private var rangeSlider: RangeUISlider!
private var minValueSelectedLabel: UILabel!
private var maxValueSelectedLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
rangeSlider = RangeUISlider(frame: CGRect(origin: CGPoint(x: 20, y: 20), size: CGSize(width: 100, height: 50)))
rangeSlider.translatesAutoresizingMaskIntoConstraints = false
rangeSlider.delegate = self
rangeSlider.scaleMinValue = 0 // If you don't set any value the default is 0
rangeSlider.scaleMaxValue = 100 // If you don't set any value the default is 1
rangeSlider.defaultValueLeftKnob = 10 // If the scale is the default one insert a value between 0 and 1
rangeSlider.defaultValueRightKnob = 75 // If the scale is the default one insert a value between 0 and 1
rangeSlider.rangeSelectedGradientColor1 = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
rangeSlider.rangeSelectedGradientColor2 = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
rangeSlider.rangeSelectedGradientStartPoint = CGPoint(x: 0, y: 0.5)
rangeSlider.rangeSelectedGradientEndPoint = CGPoint(x: 0, y: 1)
rangeSlider.rangeNotSelectedGradientColor1 = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
rangeSlider.rangeNotSelectedGradientColor2 = #colorLiteral(red: 0.09019608051, green: 0, blue: 0.3019607961, alpha: 1)
rangeSlider.rangeNotSelectedGradientStartPoint = CGPoint(x: 0, y: 0.5)
rangeSlider.rangeNotSelectedGradientEndPoint = CGPoint(x: 0, y: 1)
rangeSlider.barHeight = 20
rangeSlider.barCorners = 10
rangeSlider.leftKnobColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
rangeSlider.leftKnobWidth = 40
rangeSlider.leftKnobHeight = 40
rangeSlider.leftKnobCorners = 20
rangeSlider.rightKnobColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
rangeSlider.rightKnobWidth = 40
rangeSlider.rightKnobHeight = 40
rangeSlider.rightKnobCorners = 20
self.view.addSubview(rangeSlider)
minValueSelectedLabel = UILabel()
minValueSelectedLabel.translatesAutoresizingMaskIntoConstraints = false
minValueSelectedLabel.accessibilityIdentifier = "minValueSelected"
minValueSelectedLabel.text = "0.0"
self.view.addSubview(minValueSelectedLabel)
maxValueSelectedLabel = UILabel()
maxValueSelectedLabel.translatesAutoresizingMaskIntoConstraints = false
maxValueSelectedLabel.accessibilityIdentifier = "maxValueSelected"
maxValueSelectedLabel.text = "0.0"
self.view.addSubview(maxValueSelectedLabel)
// Setup slide with programmatic autolayout.
NSLayoutConstraint.activate([
NSLayoutConstraint(item: rangeSlider!,
attribute: .leading,
relatedBy: .equal,
toItem: self.view,
attribute: .leading,
multiplier: 1.0,
constant: 20),
NSLayoutConstraint(item: rangeSlider!,
attribute: .trailing,
relatedBy: .equal,
toItem: self.view,
attribute: .trailing,
multiplier: 1.0,
constant: -20),
NSLayoutConstraint(item: rangeSlider!,
attribute: .top,
relatedBy: .equal,
toItem: self.view,
attribute: .top,
multiplier: 1.0,
constant: 100),
NSLayoutConstraint(item: rangeSlider!,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 50),
NSLayoutConstraint(item: minValueSelectedLabel!,
attribute: .top,
relatedBy: .equal,
toItem: rangeSlider,
attribute: .bottom,
multiplier: 1.0,
constant: 20),
NSLayoutConstraint(item: minValueSelectedLabel!,
attribute: .leading,
relatedBy: .equal,
toItem: self.view,
attribute: .leading,
multiplier: 1.0,
constant: 20),
NSLayoutConstraint(item: minValueSelectedLabel!,
attribute: .trailing,
relatedBy: .equal,
toItem: self.view,
attribute: .trailing,
multiplier: 1.0,
constant: -20),
NSLayoutConstraint(item: minValueSelectedLabel!,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 30),
NSLayoutConstraint(item: maxValueSelectedLabel!,
attribute: .top,
relatedBy: .equal,
toItem: minValueSelectedLabel,
attribute: .bottom,
multiplier: 1.0,
constant: 20),
NSLayoutConstraint(item: maxValueSelectedLabel!,
attribute: .leading,
relatedBy: .equal,
toItem: self.view,
attribute: .leading,
multiplier: 1.0,
constant: 20),
NSLayoutConstraint(item: maxValueSelectedLabel!,
attribute: .trailing,
relatedBy: .equal,
toItem: self.view,
attribute: .trailing,
multiplier: 1.0,
constant: -20),
NSLayoutConstraint(item: maxValueSelectedLabel!,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 30)
])
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.rangeSlider.scaleMinValue = 50
self.rangeSlider.scaleMaxValue = 200
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.rangeSlider.changeLeftKnob(value: 60)
self.rangeSlider.changeRightKnob(value: 190)
}
}
func rangeChangeFinished(event: RangeUISliderChangeFinishedEvent) {
print("FINISH: \(event.slider.identifier)")
print("FINISH min: \(event.minValueSelected) - max: \(event.maxValueSelected)")
minValueSelectedLabel.text = event.minValueSelected.description
maxValueSelectedLabel.text = event.maxValueSelected.description
}
func rangeIsChanging(event: RangeUISliderChangeEvent) {
print("min: \(event.minValueSelected) - max: \(event.maxValueSelected)")
}
}