简体   繁体   中英

Date Picker swift iOS

So I have my date picker set up as

func configurePicker()
{
    pickerContainer.frame = CGRectMake(0.0, 600.0, 320.0, 300.0)
    pickerContainer.backgroundColor = UIColor(red: 6/225.0, green: 232/225.0, blue: 255/225.0, alpha: 0)

    picker.frame    = CGRectMake(0.0, 20.0, 320.0, 300.0)
    let dateComponents = NSDateComponents()
    dateComponents.year = -18
    let dateComponents2 = NSDateComponents()
    dateComponents2.year = -60
    picker.maximumDate = NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: NSDate(), options: nil)
    picker.minimumDate = NSCalendar.currentCalendar().dateByAddingComponents(dateComponents2, toDate: NSDate(), options: nil)
    picker.setDate(picker.maximumDate!, animated: true)
    picker.datePickerMode = UIDatePickerMode.Date
    pickerContainer.addSubview(picker)
}

as you can tell I have a minimum and maximum date set up. I also have a dismiss function I did not include. My question is how can I find out when the user is below the minimum and above maximum? What I want to do is play a gif in the background when the user is below the minimum and a separate gif when above the maximum? is there a way to tell where the user is on the date picker wheel? I understand that the date picker wheel resets past the minimum and maximum but i just want to know if there is a way to know what date the wheel is at without the user letting go. For example a conditional statement like this that works

if(picker.date > picker.maximumDate)
{
  playgif1()
}
else if(picker.date < picker.minimumDate)
{
  playgif2()
}

Above comparisons do not work gives me an error keeps saying cannot find overload for '>' or '<'

Thank You for any help

As far as I know, unless Apple added a new API, UIDatePicker (and UIPickerView in general), do not have methods to get scroll events from them.

I think the best you can do is to add a target method to the picker for a ValueChanged event, which will call a method when the user has stopped scrolling on a date.

func configurePicker() {
    //...other configuration
    picker.addTarget(self, action: "datePicked:", forControlEvents: UIControlEvents.ValueChanged)
}

func datePicked(picker: UIDatePicker) {
    // compare dates
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM