gpt4 book ai didi

ios - 是否可以让 UIDatePicker 使用开始时间和结束时间?

转载 作者:搜寻专家 更新时间:2023-11-01 06:04:41 25 4
gpt4 key购买 nike

目前我有一个像下面这样工作的 UIDatePicker:

enter image description here

但是我想让它像下面这样有开始和结束时间吗? enter image description here

有没有人在 UIDatePicker 上完成这个定制?

以这种方式使用 UIDatePicker 的应用程序是 https://www.zeel.com/

谢谢!

最佳答案

虽然 Rikn 和 Kayla 的回答为我指明了正确的方向,但他们没有像我在问题中提供的图片中那样获取日期的部分,所以这就是为什么我将代码放在这里的原因.

结果:

代码:

属性

class ViewController: UIViewController {
@IBOutlet weak var pickerView: UIPickerView!

var days = [Date]()
var startTimes = [Date]()
var endTimes = [Date]()

let dayFormatter = DateFormatter()
let timeFormatter = DateFormatter()

override func viewDidLoad() {
super.viewDidLoad()
dayFormatter.dateFormat = "EE d MMM"
timeFormatter.timeStyle = .short

days = setDays()
startTimes = setStartTimes()
endTimes = setEndTimes()
}
}

辅助方法

extension ViewController {
func getDays(of date: Date) -> [Date] {
var dates = [Date]()

let calendar = Calendar.current

// first date
var currentDate = date

// adding 30 days to current date
let oneMonthFromNow = calendar.date(byAdding: .day, value: 30, to: currentDate)

// last date
let endDate = oneMonthFromNow

while currentDate <= endDate! {
dates.append(currentDate)
currentDate = calendar.date(byAdding: .day, value: 1, to: currentDate)!
}

return dates
}

func getTimes(of date: Date) -> [Date] {
var times = [Date]()
var currentDate = date

currentDate = Calendar.current.date(bySetting: .hour, value: 7, of: currentDate)!
currentDate = Calendar.current.date(bySetting: .minute, value: 00, of: currentDate)!

let calendar = Calendar.current

let interval = 60
var nextDiff = interval - calendar.component(.minute, from: currentDate) % interval
var nextDate = calendar.date(byAdding: .minute, value: nextDiff, to: currentDate) ?? Date()

var hour = Calendar.current.component(.hour, from: nextDate)

while(hour < 23) {
times.append(nextDate)

nextDiff = interval - calendar.component(.minute, from: nextDate) % interval
nextDate = calendar.date(byAdding: .minute, value: nextDiff, to: nextDate) ?? Date()

hour = Calendar.current.component(.hour, from: nextDate)
}

return times
}

func setDays() -> [Date] {
let today = Date()
return getDays(of: today)
}

func setStartTimes() -> [Date] {
let today = Date()
return getTimes(of: today)
}

func setEndTimes() -> [Date] {
let today = Date()
return getTimes(of: today)
}

func getDayString(from: Date) -> String {
return dayFormatter.string(from: from)
}

func getTimeString(from: Date) -> String {
return timeFormatter.string(from: from)
}
}

选择器委托(delegate)

extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return days.count
case 1:
return startTimes.count
case 2:
return endTimes.count
default:
return 0
}
}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label: UILabel

if let view = view as? UILabel {
label = view
} else {
label = UILabel()
}

label.textColor = .black
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 15)

var text = ""

switch component {
case 0:
text = getDayString(from: days[row])
case 1:
text = getTimeString(from: startTimes[row])
case 2:
text = getTimeString(from: endTimes[row])
default:
break
}

label.text = text

return label
}
}

关于ios - 是否可以让 UIDatePicker 使用开始时间和结束时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40878547/

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