我正在研究范围滑块的实现:https://github.com/warchimede/RangeSlider我看到它有一个lowerValue和upperValue默认设置为0.2和0.8:
@IBInspectable var lowerValue: Double = 0.2 {
didSet {
if lowerValue < minimumValue {
lowerValue = minimumValue
}
updateLayerFrames()
}
}
@IBInspectable var upperValue: Double = 0.8 {
didSet {
if upperValue > maximumValue {
upperValue = maximumValue
}
updateLayerFrames()
}
}
它还将minimumValue和maximumValue设置为0和1:
@IBInspectable var minimumValue: Double = 0.0 {
willSet(newValue) {
assert(newValue < maximumValue, "RangeSlider: minimumValue should be lower than maximumValue")
}
didSet {
updateLayerFrames()
}
}
@IBInspectable var maximumValue: Double = 1.0 {
willSet(newValue) {
assert(newValue > minimumValue, "RangeSlider: maximumValue should be greater than minimumValue")
}
didSet {
updateLayerFrames()
}
}
我想更改此滑块的行为,使其成为范围日期选择器 - maximumValue应该是当前日期,minimumValue应该是过去的某个日期,例如6个月前 你能帮我解决这个问题,并提供一些提示是否可以使用这个范围滑块吗?
答案 0 :(得分:3)
这应该这样做。
class ViewController: UIViewController {
var slider:UISlider?
// These number values represent each slider position
let daysInPast = [14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] //Add your values here - this sets from two past weeks to current day
let secondsPerDay = 86400
var oldIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
slider = UISlider(frame: self.view.bounds)
self.view.addSubview(slider!)
// slider values go from 0 to the number of values in your numbers array
var numberOfSteps = Float(numbers.count - 1)
slider!.maximumValue = numberOfSteps;
slider!.minimumValue = 0;
// As the slider moves it will continously call the -valueChanged:
slider!.continuous = true; // false makes it call only once you let go
slider!.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged)
}
func valueChanged(sender: UISlider) {
// round the slider position to the nearest index of the numbers array
var index = (Int)(slider!.value + 0.5);
slider?.setValue(Float(index), animated: false)
var dayInPast = daysInPast[index]; // <-- This numeric value you want
if oldIndex != index {
let today = NSDate() //today
//every day value in the slider will be multiplied by number of seconds in day
let pickedDate = today.dateByAddingTimeInterval(-secondsPerDay * dayInPast)
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Month, .Year, .Day], fromDate: pickedDate)
let month = components.month
let year = components.year
let day = components.day
print("Date picked with slider is \(month)/\(day), \(year)")
oldIndex = index
}
}
}