I want to Switch Time scrubber to do 15 min interv

2019-03-07 00:35发布

Please Suggest me i want to go in 15 minute intervals. ex: 4:00, 4:15, 4:30, 4:45 and so next.

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

I have sliderMoved that starting point is 8:00 AM and end point 9:00 PM i have set slider min value 28,800 and max value 75,600, When i slide the slider the value is change with my timeFromSlot method and the changed value i got in between 8:01 8:02 8:03.. and i want changed value 8:00 8:15 8:30 like this.

2条回答
我命由我不由天
2楼-- · 2019-03-07 01:12

It should be something like

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

}
查看更多
倾城 Initia
3楼-- · 2019-03-07 01:14

For you case, it's better to use some portions scaling. try this code below. I believe it's pretty straightforward.

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)
    }
}
查看更多
登录 后发表回答