如何在 SwiftUI 的 DatePicker 中设置 countDownTimer 模式? [英] How can I set countDownTimer mode in DatePicker on SwiftUI?

查看:21
本文介绍了如何在 SwiftUI 的 DatePicker 中设置 countDownTimer 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

使用 SwiftUI,我看不到任何解决此问题的本机方法.唯一的方法是与 UIKit 交互?

With UIKit, the UIDatePicker allow set mode to UIDatePicker.Mode.countDownTimer and we can have set duration.

With SwiftUI, I don't see any native way to solve this. The only way would be doing interface with UIKit?

[更新]
仅找到解决方案

[UPDATE]
Only solution found

DurationPickerView.swift

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))
        }
    }
}

时间.swift

import Foundation

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

推荐答案

现在获得倒计时行为的唯一方法是将 UIDatePicker 包装在自定义视图中.

The only way to get the countdown behavior right now is by wrapping UIDatePicker in a custom view.

这是使用 countDownDurationAilton Vieira Pinto Filho 的代码的简化版本.

Here is a simplified version of Ailton Vieira Pinto Filho's code using countDownDuration.

import SwiftUI

struct DurationPicker: UIViewRepresentable {
    @Binding var duration: TimeInterval

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

    func updateUIView(_ datePicker: UIDatePicker, context: Context) {
        datePicker.countDownDuration = duration
    }

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

    class Coordinator: NSObject {
        let parent: DurationPicker

        init(_ parent: DurationPicker) {
            self.parent = parent
        }

        @objc func updateDuration(datePicker: UIDatePicker) {
            parent.duration = datePicker.countDownDuration
        }
    }
}

这篇关于如何在 SwiftUI 的 DatePicker 中设置 countDownTimer 模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆