我想切换时间洗涤器以 15 分钟为间隔
I want to Switch Time scrubber to do 15 min intervals
请建议我每 15 分钟去一次。
例如:4:00、4:15、4:30、4:45 等等。
@IBAction func sliderMoved(sender: UISlider) {
let slot = Double(sender.value)
let slotDate = self.timeFromSlot(slot)
print(timeFormatter.stringFromDate(slotDate!))
}
private func timeFromSlot(slot:Double) -> NSDate? {
let cal = NSCalendar.currentCalendar()
let unit:NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second]
let comps = cal.components(unit, fromDate: NSDate())
comps.hour = Int((slot % (24 * 3600)) / 3600)
comps.minute = Int(slot % 3600 / 60)
comps.second = 0
let startedDate = cal.dateFromComponents(comps)!
return startedDate
}
我有 sliderMoved,起点是 8:00 AM,终点是 9:00 PM 我已经设置了滑块最小值 28,800 和最大值 75,600,当我滑动滑块时,值会随着我的变化而变化timeFromSlot 方法和我在 8:01 8:02 8:03 之间获得的更改值,我想要像这样更改值 8:00 8:15 8:30。
应该是
yourSlider.continuous = false
@IBAction func sliderMoved(sender: UISlider) {
let slot = Double(sender.value)
let slotDate = self.timeFromSlot(slot)
print(timeFormatter.stringFromDate(slotDate!))
if (sender.value >= 28800 && sender.value < your next interval 1) {
sender.value = 28800
} else if (sender.value >= your next interval 1 && sender.value < your next interval 2) {
sender.value = your next interval 1
} else if ....
}
对于你的情况,最好使用一些部分缩放。尝试下面的代码。我相信这很简单。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var label: UILabel!
let ticksCount: Float = 13*4 // 15mins intervals count from 8am till 9pm
let calendar = NSCalendar.currentCalendar()
let todayDate = NSDate()
override func viewDidLoad() {
super.viewDidLoad()
slider.minimumValue = 0
slider.maximumValue = ticksCount
}
@IBAction func onValueChanged(slider: UISlider) {
let minutesFromDayStart = 8 * 60 + Int(slider.value) * 15 // total minutes from day start, assuming we use intervals from 8am
let timeSlotDate = dateWithTimeSlotForDate(todayDate, minutesFromDayStart: minutesFromDayStart)
showDate(timeSlotDate)
}
private func showDate(date: NSDate) {
let df = NSDateFormatter()
df.dateStyle = .MediumStyle
df.timeStyle = .MediumStyle
label.text = df.stringFromDate(date)
}
private func dateWithTimeSlotForDate(date: NSDate, minutesFromDayStart: Int) -> NSDate {
let dayStart = dayStartDateForDate(date)
return calendar.dateByAddingUnit(.Minute, value: minutesFromDayStart, toDate: dayStart, options: NSCalendarOptions(rawValue: 0))!
}
private func dayStartDateForDate(date: NSDate) -> NSDate {
return calendar.startOfDayForDate(date)
}
}
请建议我每 15 分钟去一次。 例如:4:00、4:15、4:30、4:45 等等。
@IBAction func sliderMoved(sender: UISlider) {
let slot = Double(sender.value)
let slotDate = self.timeFromSlot(slot)
print(timeFormatter.stringFromDate(slotDate!))
}
private func timeFromSlot(slot:Double) -> NSDate? {
let cal = NSCalendar.currentCalendar()
let unit:NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second]
let comps = cal.components(unit, fromDate: NSDate())
comps.hour = Int((slot % (24 * 3600)) / 3600)
comps.minute = Int(slot % 3600 / 60)
comps.second = 0
let startedDate = cal.dateFromComponents(comps)!
return startedDate
}
我有 sliderMoved,起点是 8:00 AM,终点是 9:00 PM 我已经设置了滑块最小值 28,800 和最大值 75,600,当我滑动滑块时,值会随着我的变化而变化timeFromSlot 方法和我在 8:01 8:02 8:03 之间获得的更改值,我想要像这样更改值 8:00 8:15 8:30。
应该是
yourSlider.continuous = false
@IBAction func sliderMoved(sender: UISlider) {
let slot = Double(sender.value)
let slotDate = self.timeFromSlot(slot)
print(timeFormatter.stringFromDate(slotDate!))
if (sender.value >= 28800 && sender.value < your next interval 1) {
sender.value = 28800
} else if (sender.value >= your next interval 1 && sender.value < your next interval 2) {
sender.value = your next interval 1
} else if ....
}
对于你的情况,最好使用一些部分缩放。尝试下面的代码。我相信这很简单。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var label: UILabel!
let ticksCount: Float = 13*4 // 15mins intervals count from 8am till 9pm
let calendar = NSCalendar.currentCalendar()
let todayDate = NSDate()
override func viewDidLoad() {
super.viewDidLoad()
slider.minimumValue = 0
slider.maximumValue = ticksCount
}
@IBAction func onValueChanged(slider: UISlider) {
let minutesFromDayStart = 8 * 60 + Int(slider.value) * 15 // total minutes from day start, assuming we use intervals from 8am
let timeSlotDate = dateWithTimeSlotForDate(todayDate, minutesFromDayStart: minutesFromDayStart)
showDate(timeSlotDate)
}
private func showDate(date: NSDate) {
let df = NSDateFormatter()
df.dateStyle = .MediumStyle
df.timeStyle = .MediumStyle
label.text = df.stringFromDate(date)
}
private func dateWithTimeSlotForDate(date: NSDate, minutesFromDayStart: Int) -> NSDate {
let dayStart = dayStartDateForDate(date)
return calendar.dateByAddingUnit(.Minute, value: minutesFromDayStart, toDate: dayStart, options: NSCalendarOptions(rawValue: 0))!
}
private func dayStartDateForDate(date: NSDate) -> NSDate {
return calendar.startOfDayForDate(date)
}
}