gpt4 book ai didi

ios - 如何在 Swift 中使用带有日期而不是数字的范围 slider ?

转载 作者:行者123 更新时间:2023-11-28 11:03:48 24 4
gpt4 key购买 nike

我正在查看范围 slider 的实现: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()
}
}

我想更改此 slider 的行为,使其成为范围日期选择器 - maximumValue 应为当前日期,minimumValue 应为过去某个日期,例如6个月前。你能帮我解决这个问题,并给出一些提示是否可以使用这个范围 slider 来做到这一点?

最佳答案

应该这样做。

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
}
}
}

关于ios - 如何在 Swift 中使用带有日期而不是数字的范围 slider ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39466038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com