0

Just implemented this post : Slider with real time in Label

With Denis code and works fine. Thanks.

Just translated to Swift, if anybody want:

func timeSlider(aSlider: UISlider)-> String {
    let numberOfSlots = 24*4 - 1
    let dateFormat = NSDateFormatter()
    dateFormat.dateFormat = "h-mm"
    let zeroDate = dateFormat.dateFromString("00-00")
    let actualSlot = roundf(Float(numberOfSlots)*Float(aSlider.value))
    let slotInterval:NSTimeInterval = NSTimeInterval(actualSlot * 15 * 60)
    let slotDate: NSDate = NSDate(timeInterval: slotInterval, sinceDate: zeroDate!)
    dateFormat.dateFormat = "h-mm"
    return dateFormat.stringFromDate(slotDate)
}

Now I'm trying to figure out how make the inverse operation. I mean, in my database I'll store the time, shown on label. When I query the database, how can I set the slider ?

My slider is showing values like:

2015-08-21 20:46:14.831 [5306:1205420] 0.303571

2015-08-21 20:46:14.847 [5306:1205420] 0.292411

2015-08-21 20:46:14.864 [5306:1205420] 0.276786

While my slider has minumum = 0 and maximum = 1

Any help ?

Thank you

Community
  • 1
  • 1
GIJOW
  • 2,307
  • 1
  • 17
  • 37

1 Answers1

1

UISlider has a method - setValue:animated: that you can use to move the slider

Here is the documentation

----EDIT----

In order to convert the time value to a value appliable to the slider you need to do the following:

(dbTime - minTime) / (maxTime - minTime)

as long as the dbTime is inbetween the min and max value, it will give a value between 0 and 1

Leonardo
  • 1,740
  • 18
  • 27
  • Thats not my question buddy. How do I set time value to a slider which has min value 0 and max value 1 ? – GIJOW Aug 21 '15 at 20:00
  • you have a minimum and maximum time right? and want to set the slider to a time inbetween the two? – Leonardo Aug 21 '15 at 20:10
  • yes from 0:00 to 23:45, slider from 0 to 1, for example, 0.276786 = 9:45. I would store in my DB 9:45, but to reload data inside the app I need to re-convert 9:45 in 0.276786. Got it ? Thank you – GIJOW Aug 21 '15 at 20:44
  • Sorry to revive this question, but in your function we would have I.e (9:45-0:00)/(23:45-00:00) always will be DB time divided by 23:45 right ? Don't know if I got it but the result is not good ;-) thank you – GIJOW Oct 07 '15 at 04:49
  • Not sure I'm understanding your problem. In a slider from 0 to 1, 0 being equivalent to 0:00 and 1 equivalent to 23:45, 9:45 would be 0,4105263158 – Leonardo Oct 07 '15 at 18:26