//
|
// TaxiReservationView.swift
|
// OKProject
|
//
|
// Created by alvin_y on 2020/6/2.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
import SwiftDate
|
/// 出租车预约时间
|
class TaxiReservationView: UIView {
|
|
/// 容器
|
private let view_container: UIView = {
|
let view = UIView()
|
view.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#FFFFFF"), dark: UIColor.color(hexString: "#2C2C2C"))
|
view.layer.masksToBounds = true
|
view.layer.cornerRadius = 4
|
return view
|
}()
|
|
/// 标题
|
private let label_title: UILabel = {
|
let label = UILabel()
|
label.attributedText = AttributedStringbuilder.build()
|
.add(string: "选择用车时间", withFont: UIFont.systemFont(ofSize: 16, weight: .medium), withColor: UIColor.color(light: UIColor.color(hexString: "#000000"), dark: UIColor.color(hexString: "#FFFFFF")))
|
.add(string: "(上下滑动选择)", withFont: UIFont.systemFont(ofSize: 12), withColor: UIColor.color(hexString: "#000000",0.6))
|
.mutableAttributedString
|
return label
|
}()
|
var title:String?{
|
didSet{
|
self.label_title.attributedText = AttributedStringbuilder.build()
|
.add(string: title!, withFont: UIFont.systemFont(ofSize: 16, weight: .medium), withColor: UIColor.color(light: UIColor.color(hexString: "#000000"), dark: UIColor.color(hexString: "#FFFFFF")))
|
.add(string: "(上下滑动选择)", withFont: UIFont.systemFont(ofSize: 12), withColor: UIColor.color(hexString: "#000000",0.6))
|
.mutableAttributedString
|
}
|
}
|
|
/// 确认
|
private let button_submit: UIButton = {
|
let button = UIButton()
|
button.setTitle("确认", for: .normal)
|
button.setTitleColor(UIColor.color(light: UIColor.color(hexString: "#FFFFFF"), dark: UIColor.color(hexString: "#FFFFFF")), for: .normal)
|
button.titleLabel?.font = UIFont.init(name: Medium, size: 15)
|
button.layer.masksToBounds = true
|
button.layer.cornerRadius = 4
|
button.backgroundColor = ThemeColor
|
return button
|
}()
|
|
private var limitMinute:Int?
|
|
/// 时间选择器
|
private let pickView = UIPickerView()
|
|
/// 天数
|
private var days = [String]()
|
|
/// 小时
|
private var hours = [String]()
|
|
/// 分钟
|
private var minutes = [String]()
|
|
/// 时间间隔
|
private var interval: Int = 1
|
|
private var intervalMinute:Double!
|
|
private var extraDay:Int = 1
|
|
/// 当前时间
|
private let date = DateInRegion(Date(), region: Region(calendar: Calendars.gregorian, zone: Zones.current,locale: Locales.current))
|
|
/// 时间回调 time时间戳 string时间字符串
|
let complete = Delegate<Double?,Void>()
|
|
override init(frame: CGRect) {
|
super.init(frame: frame)
|
setupViews()
|
defineLayouts()
|
bindRx()
|
}
|
|
/// 显示
|
func show(intervalMinute:Double = 10,extraDay:Int = 1) {
|
self.intervalMinute = intervalMinute
|
self.extraDay = extraDay
|
app.window?.addSubview(self)
|
setupDays()
|
setupHour()
|
setupMinute()
|
UIView.animate(withDuration: 0.2) {
|
self.view_container.transform = CGAffineTransform.identity
|
}
|
// 默认选中
|
pickView.selectRow(1, inComponent: 0, animated: true)
|
setupHour()
|
setupMinute()
|
pickView.reloadComponent(1)
|
pickView.selectRow(0, inComponent: 1, animated: false)
|
pickView.reloadComponent(2)
|
pickView.selectRow(0, inComponent: 2, animated: false)
|
}
|
|
/// 隐藏
|
func dismiss() {
|
UIView.animate(withDuration: 0.2, animations: {
|
self.view_container.transform = CGAffineTransform.init(translationX: 0, y: screenH)
|
}) { (_) in
|
self.removeFromSuperview()
|
}
|
|
}
|
|
/// 获取天数
|
private func setupDays() {
|
days.removeAll()
|
days.append("现在")
|
// 判断是否可以选择今天
|
if date.hour == 23 && date.minute < 50{
|
for item in 0...extraDay{
|
let temp = (date.date + item.days).toFormat("yyyy-MM-dd")
|
days.append(temp)
|
}
|
}else if date.hour < 23{
|
for item in 0...extraDay{
|
let temp = (date.date + item.days).toFormat("yyyy-MM-dd")
|
days.append(temp)
|
}
|
}else{
|
for item in 1...extraDay{
|
let temp = (date.date + item.days).toFormat("yyyy-MM-dd")
|
days.append(temp)
|
}
|
}
|
pickView.reloadComponent(0)
|
}
|
|
/// 获取小时
|
private func setupHour() {
|
if pickView.selectedRow(inComponent: 0) == 0{
|
hours.removeAll()
|
hours.append("--")
|
pickView.reloadComponent(1)
|
}else if pickView.selectedRow(inComponent: 0) == 1{
|
hours.removeAll()
|
// if date.minute > 0 && (date.hour + 1) < 23{
|
// for item in (date.hour + 1)...23 {
|
// let string = String.init(format: "%02d点", item)
|
// hours.append(string)
|
// }
|
// }else{
|
// // 跨天了
|
// if date.hour == 23 && date.minute > 0{
|
// for item in 0...23 {
|
// let string = String.init(format: "%02d点", item)
|
// hours.append(string)
|
// }
|
// }else{
|
// for item in date.hour...23 {
|
// let string = String.init(format: "%02d点", item)
|
// hours.append(string)
|
// }
|
// }
|
//
|
// }
|
|
if date.hour == 23 && date.minute >= 40{
|
|
}
|
|
if date.minute > 49 && date.hour == 23{
|
for item in 0...23 {
|
let string = String.init(format: "%02d点", item)
|
hours.append(string)
|
}
|
}else if date.hour < 23 && date.minute > 49{
|
for item in (date.hour + 1)...23 {
|
let string = String.init(format: "%02d点", item)
|
hours.append(string)
|
}
|
}else{
|
for item in date.hour...23 {
|
let string = String.init(format: "%02d点", item)
|
hours.append(string)
|
}
|
}
|
pickView.reloadComponent(1)
|
}else{
|
hours.removeAll()
|
for item in 0...23 {
|
let string = String.init(format: "%02d点", item)
|
hours.append(string)
|
}
|
pickView.reloadComponent(1)
|
}
|
}
|
|
/// 获取分钟
|
private func setupMinute() {
|
if pickView.selectedRow(inComponent: 1) == 0 && pickView.selectedRow(inComponent: 0) == 0{
|
minutes.removeAll()
|
minutes.append("--")
|
pickView.reloadComponent(2)
|
}else if pickView.selectedRow(inComponent: 1) == 0 && pickView.selectedRow(inComponent: 0) == 1{
|
var minute = 0
|
minutes.removeAll()
|
// 多少分钟之后
|
//73976 [专车]时间只能选择当前时间后十分钟后的时间,与原型不符
|
// let intervalMinute: Double = 10
|
let calendar = Calendar.current
|
let unit: Set<Calendar.Component> = [Calendar.Component.hour , Calendar.Component.minute]
|
let nowDateComponents: DateComponents = calendar.dateComponents(unit, from: Date())
|
let futureDateComponents = calendar.dateComponents(unit, from: Date.init(timeIntervalSinceNow: 60 * intervalMinute))
|
if (nowDateComponents.hour != futureDateComponents.hour ||
|
futureDateComponents.minute ?? 0 > 60 - 1) {
|
if (futureDateComponents.minute ?? 0) % interval != 0 {
|
let tmpInterger = (futureDateComponents.minute ?? 0) + interval - (futureDateComponents.minute ?? 0) % interval
|
if tmpInterger == 60 {
|
minute = 0
|
}else {
|
minute = tmpInterger
|
}
|
}else{
|
minute = (futureDateComponents.minute ?? 0)
|
}
|
}else{
|
minute = (futureDateComponents.minute ?? 0)
|
if minute % interval != 0{
|
let tmpInteger = minute + interval - minute % interval
|
minute = tmpInteger
|
}
|
}
|
|
for item in stride(from: minute, to: 60, by: 1){
|
minutes.append(String.init(format: "%02ld分", item))
|
}
|
pickView.reloadComponent(2)
|
}else{
|
minutes.removeAll()
|
for item in stride(from: 0, to: 60, by: 1){
|
minutes.append(String.init(format: "%02ld分", item))
|
}
|
pickView.reloadComponent(2)
|
}
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
//MARK: - UI
|
private func setupViews() {
|
self.frame = UIScreen.main.bounds
|
self.view_container.transform = CGAffineTransform.init(translationX: 0, y: screenH)
|
self.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#000000",0.5), dark: UIColor.color(hexString: "#000000",0.61))
|
self.addSubview(view_container)
|
view_container.addSubview(label_title)
|
view_container.addSubview(button_submit)
|
view_container.addSubview(pickView)
|
pickView.delegate = self
|
pickView.dataSource = self
|
}
|
|
//MARK: - Layouts
|
private func defineLayouts() {
|
view_container.snp.makeConstraints { (make) in
|
make.left.equalToSuperview().offset(14)
|
make.right.equalToSuperview().offset(-14)
|
make.bottom.equalToSuperview().offset(-17)
|
make.height.equalTo(236)
|
}
|
label_title.snp.makeConstraints { (make) in
|
make.centerX.equalToSuperview()
|
make.height.equalTo(22)
|
make.top.equalToSuperview().offset(18)
|
}
|
button_submit.snp.makeConstraints { (make) in
|
make.centerX.equalToSuperview()
|
make.bottom.equalToSuperview().offset(-12)
|
make.left.equalToSuperview().offset(40)
|
make.right.equalToSuperview().offset(-40)
|
make.height.equalTo(40)
|
}
|
pickView.snp.makeConstraints { (make) in
|
make.top.equalTo(label_title.snp.bottom).offset(20)
|
make.bottom.equalTo(button_submit.snp.top).offset(-16)
|
make.left.equalToSuperview()
|
make.right.equalToSuperview()
|
}
|
}
|
|
|
//MARK: - Rx
|
private func bindRx() {
|
button_submit.rx.tap.subscribe(onNext: {[unowned self] (_) in
|
let one = self.days[self.pickView.selectedRow(inComponent: 0)]
|
let two = self.hours[self.pickView.selectedRow(inComponent: 1)]
|
let three = self.minutes[self.pickView.selectedRow(inComponent: 2)]
|
if one == "现在"{
|
self.dismiss()
|
self.complete.call(nil)
|
}else{
|
let year = one
|
let date = "\(year) \(two.substring(to: 2)):\(three.substring(to: 2))".toDate("yyyy-MM-dd HH:mm", region: .current)?.date
|
guard let d = date else {
|
alert(text: "时间错误")
|
return
|
}
|
self.dismiss()
|
self.complete.call(Double(d.timeIntervalSince1970))
|
}
|
|
}).disposed(by: rx.disposeBag)
|
}
|
|
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
var point: CGPoint? = touches.first?.location(in: self)
|
point = view_container.layer.convert(point ?? CGPoint.zero, from: self.layer)
|
if view_container.layer.contains(point ?? CGPoint.zero) {
|
//处理点击到这个view中要执行的事件
|
return
|
}
|
super.touchesBegan(touches, with: event)
|
self.dismiss()
|
}
|
}
|
// MARK: - UIPickerViewDelegate
|
extension TaxiReservationView:UIPickerViewDelegate,UIPickerViewDataSource{
|
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
if component == 0 {
|
return days.count
|
}else if component == 1{
|
return hours.count
|
}else{
|
return minutes.count
|
}
|
}
|
|
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
return 3
|
}
|
|
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
|
//时间表的线
|
for singleLine in pickerView.subviews {
|
if singleLine.frame.size.height < 2{
|
singleLine.isHidden = true
|
}
|
}
|
|
let label:UILabel = view as? UILabel ?? UILabel()
|
label.font = UIFont.boldSystemFont(ofSize: 16)
|
label.textAlignment = .center
|
label.adjustsFontSizeToFitWidth = true
|
if component == 0 {
|
if days[row] == "现在"{
|
label.text = "现在"
|
}else{
|
label.text = days[row].toDate(fromFormat: "yyyy-MM-dd", toFormat: "MM月dd日")
|
}
|
|
} else if component == 1 {
|
label.text = "\(hours[row])"
|
}else{
|
label.text = "\(minutes[row])"
|
}
|
return label
|
}
|
|
|
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
|
return (screenW - 52) / 3
|
}
|
|
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
|
return 50
|
}
|
|
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
if component == 0{
|
setupHour()
|
setupMinute()
|
pickView.reloadComponent(1)
|
pickView.selectRow(0, inComponent: 1, animated: false)
|
pickView.reloadComponent(2)
|
pickView.selectRow(0, inComponent: 2, animated: false)
|
}else if(component == 1){
|
setupMinute()
|
pickView.reloadComponent(2)
|
pickView.selectRow(0, inComponent: 2, animated: false)
|
}else{
|
|
}
|
}
|
}
|