-
Notifications
You must be signed in to change notification settings - Fork 0
/
LFSlider.swift
111 lines (96 loc) · 3.15 KB
/
LFSlider.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
//
// LFSlider.swift
// LFSlider
//
// Created by Lucas Farah on 3/12/16.
// Copyright © 2016 Lucas Farah. All rights reserved.
//
import UIKit
class LFSlider: UIView {
var height: CGFloat?
var width: CGFloat?
var label: UILabel?
var slider: UISlider?
var color: UIColor?
{
didSet
{
self.label?.textColor = self.color
self.slider?.thumbTintColor = self.color
}
}
override init(frame:CGRect)
{
super.init(frame: CGRect(x: 100, y: 100, width: frame.width, height: frame.height))
self.height = frame.height
self.width = frame.width
self.slider = UISlider(frame: CGRect(x: 0, y: 0, width: self.frame.width * 0.6, height: self.frame.height))
self.slider!.maximumValue = 100
self.slider!.value = 50
self.slider!.addTarget(self, action: #selector(LFSlider.sliderValueChanged(_:)), forControlEvents: .ValueChanged)
self.addSubview(self.slider!)
self.label = UILabel(frame: CGRect(x: self.slider!.frame.width + (self.frame.width * 0.1), y: 0, width: self.frame.width - self.slider!.frame.width - (self.frame.width * 0.1), height: self.frame.height))
self.label!.text = "\(Int(self.slider!.value))%"
self.label!.adjustsFontSizeToFitWidth = true
self.addSubview(self.label!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func sliderValueChanged(sender: UISlider)
{
self.label?.text = "\(Int(sender.value))%"
}
func setValue(value: Float, duration: Double = 4) {
self.slider?.setValue(value, duration: duration)
self.label?.setValue(Int(value), duration: duration)
}
}
extension NSTimer
{
public static func runThisEvery(seconds seconds: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
let fireDate = CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, seconds, 0, 0, handler)
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
return timer
}
}
extension UILabel
{
func setValue(number: Int, duration: Double)
{
if var currentNumber = Int(self.text!.substringWithRange(Range<String.Index>(self.text!.startIndex ..< self.text!.endIndex.advancedBy(-1))))
{
let timerNumber = currentNumber
NSTimer.runThisEvery(seconds: duration / Double(abs(number - timerNumber))) { (timer) -> Void in
if currentNumber == number
{
timer.invalidate()
}
else if currentNumber < number
{
currentNumber += 1
self.text = "\(currentNumber)%"
}
else if currentNumber > number
{
currentNumber -= 1
self.text = "\(currentNumber)%"
}
}
}
}
}
extension UISlider
{
/// EZSE: Slider moving to value with animation duration
public func setValue(value: Float, duration: Double) {
UIView.animateWithDuration(duration, animations: { () -> Void in
self.setValue(self.value, animated: true)
}) { (bol) -> Void in
UIView.animateWithDuration(duration, animations: { () -> Void in
self.setValue(value, animated: true)
}, completion: nil)
}
}
}