如何在SwiftUI的DatePicker中设置countDownTimer模式?

时间:2019-10-26 20:28:36

标签: swift swiftui uidatepicker

使用UIKit,UIDatePicker允许将模式设置为UIDatePicker.Mode.countDownTimer,我们可以设置持续时间。

使用SwiftUI,我看不到任何解决该问题的本地方法。 唯一的方法是与UIKit建立接口吗?

This is how I need

[更新]
仅找到解决方案

DurationPickerView.swift

import SwiftUI
import UIKit

struct DurationPickerView: UIViewRepresentable {
    @Binding var time: Time

    func makeCoordinator() -> DurationPickerView.Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> UIDatePicker {
        let datePicker = UIDatePicker()
        datePicker.datePickerMode = .countDownTimer
        datePicker.addTarget(context.coordinator, action: #selector(Coordinator.onDateChanged), for: .valueChanged)
        return datePicker
    }

    func updateUIView(_ datePicker: UIDatePicker, context: Context) {
        let date = Calendar.current.date(bySettingHour: time.hour, minute: time.minute, second: time.second, of: datePicker.date)!
        datePicker.setDate(date, animated: true)
    }

    class Coordinator: NSObject {
        var durationPicker: DurationPickerView

        init(_ durationPicker: DurationPickerView) {
            self.durationPicker = durationPicker
        }

        @objc func onDateChanged(sender: UIDatePicker) {
            print(sender.date)
            let calendar = Calendar.current
            let date = sender.date
            durationPicker.time = Time(hour: calendar.component(.hour, from: date), minute: calendar.component(.minute, from: date), second: calendar.component(.second, from: date))
        }
    }
}

Time.swift

import Foundation

struct Time {
    var hour: Int
    var minute: Int
    var second: Int = 0
}

1 个答案:

答案 0 :(得分:4)

现在获得倒计时行为的唯一方法是将 <div class="container"> <div class="row"> <div class="col"> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">Navbar</a> <!-- Will be shown for bigger viewports --> <div class="d-none d-md-block d-xl-block">Header viewport md + xl</div> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarTogglerDemo02"> <ul class="navbar-nav mr-auto mt-2 mt-lg-0"> <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a> </li> </ul> <form class="form-inline my-2 my-lg-0"> <input class="form-control mr-sm-2" type="search" placeholder="Search"> <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> </form> </div> </nav> <!-- Will be shown for smaller viewports --> <div class="d-block d-sm-block d-xs-block d-md-none d-lg-none d-xl-none">Header viewport sm + xs</div> </div> </div> </div> 包装在自定义视图中。

这是使用UIDatePicker的{​​{3}}代码的简化版本。

countDownDuration