{"id":589,"date":"2023-02-25T19:52:53","date_gmt":"2023-02-25T11:52:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=589"},"modified":"2023-04-29T20:30:08","modified_gmt":"2023-04-29T12:30:08","slug":"swift-ui-implement-countdown-function-using-uidatepicker","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-implement-countdown-function-using-uidatepicker\/","title":{"rendered":"Swift UI &#8211; \u4f7f\u7528UIDatePicker\u5b9e\u73b0\u5012\u8ba1\u65f6\u529f\u80fd"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<p>\u5982\u679c\u4f7f\u7528UIDatePicker\u65f6\u5c06\u6a21\u5f0f\u8bbe\u7f6e\u4e3aCountDownTimer\uff0c\u5373\u53ef\u8ba9\u8be5\u63a7\u4ef6\u4f5c\u4e3a\u5012\u8ba1\u65f6\u5668\u6765\u4f7f\u7528\u3002<\/p>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController {\n\n    var ctimer:UIDatePicker!\n    var btnStart:UIButton!\n\n    var leftTime:Int = 180\n    var alertController:UIAlertController!\n\n    var timer:Timer!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        ctimer = UIDatePicker(frame:CGRect(x:0, y:120, width:200, height:200))\n        self.ctimer.datePickerMode = UIDatePickerMode.countDownTimer\n\n        \/\/\u5fc5\u987b\u4e3a60\u7684\u6574\u6570\u500d\uff0c\u6bd4\u5982\u8bbe\u7f6e\u4e3a100\uff0c\u503c\u81ea\u52a8\u53d8\u4e3a60\n        self.ctimer.countDownDuration = TimeInterval(leftTime)\n        ctimer.addTarget(self, action: #selector(ViewController.timerChanged),\n                        for: .valueChanged)\n\n        self.view.addSubview(ctimer)\n\n        btnStart =  UIButton(type: .system)\n        btnStart.frame = CGRect(x:100, y:400, width:100, height:100);\n        btnStart.setTitleColor(UIColor.red, for: .normal)\n        btnStart.setTitleColor(UIColor.green, for:.disabled)\n        btnStart.setTitle(&quot;\u5f00\u59cb&quot;, for:.normal)\n        btnStart.setTitle(&quot;\u5012\u8ba1\u65f6\u4e2d&quot;, for:.disabled)\n\n        btnStart.clipsToBounds = true\n        btnStart.layer.cornerRadius = 5\n        btnStart.addTarget(self, action:#selector(ViewController.startClicked),\n                           for:.touchUpInside)\n\n        self.view.addSubview(btnStart)\n    }\n\n    @objc func timerChanged() {\n        print(&quot;\u60a8\u9009\u62e9\u5012\u8ba1\u65f6\u95f4\u4e3a\uff1a\\(self.ctimer.countDownDuration)&quot;)\n    }\n\n    \/**\n     * \u5f00\u59cb\u5012\u8ba1\u65f6\u6309\u94ae\u70b9\u51fb\n     *\/\n    @objc func startClicked(sender:UIButton) {\n        self.btnStart.isEnabled = false\n\n        \/\/ \u83b7\u53d6\u8be5\u5012\u8ba1\u65f6\u5668\u7684\u5269\u4f59\u65f6\u95f4\n        leftTime = Int(self.ctimer.countDownDuration);\n        \/\/ \u7981\u7528UIDatePicker\u63a7\u4ef6\u548c\u6309\u94ae\n        self.ctimer.isEnabled = false\n\n        \/\/ \u521b\u5efa\u4e00\u4e2aUIAlertController\u5bf9\u8c61\uff08\u8b66\u544a\u6846\uff09\uff0c\u5e76\u786e\u8ba4\uff0c\u5012\u8ba1\u65f6\u5f00\u59cb\n        alertController = UIAlertController(title: &quot;\u7cfb\u7edf\u63d0\u793a&quot;,\n                                            message: &quot;\u5012\u8ba1\u65f6\u5f00\u59cb\uff0c\u8fd8\u6709 \\(leftTime) \u79d2...&quot;,\n                                            preferredStyle: .alert)\n        let cancelAction = UIAlertAction(title: &quot;\u786e\u5b9a&quot;, style: .cancel, handler: nil)\n        alertController.addAction(cancelAction)\n        \/\/ \u663e\u793aUIAlertController\u7ec4\u4ef6\n        self.present(alertController, animated: true, completion: nil)\n\n        \/\/ \u542f\u7528\u8ba1\u65f6\u5668\uff0c\u63a7\u5236\u6bcf\u79d2\u6267\u884c\u4e00\u6b21tickDown\u65b9\u6cd5\n        timer = Timer.scheduledTimer(timeInterval: TimeInterval(1), target:self,\n                                    selector:#selector(ViewController.tickDown),\n                                    userInfo:nil, repeats:true)\n    }\n\n    \/**\n     * \u8ba1\u65f6\u5668\u6bcf\u79d2\u89e6\u53d1\u4e8b\u4ef6\n     *\/\n    @objc func tickDown() {\n        alertController.message = &quot;\u5012\u8ba1\u65f6\u5f00\u59cb\uff0c\u8fd8\u6709 \\(leftTime) \u79d2...&quot;\n        \/\/ \u5c06\u5269\u4f59\u65f6\u95f4\u51cf\u5c111\u79d2\n        leftTime -= 1;\n        \/\/ \u4fee\u6539UIDatePicker\u7684\u5269\u4f59\u65f6\u95f4\n        self.ctimer.countDownDuration = TimeInterval(leftTime);\n        print(leftTime)\n        \/\/ \u5982\u679c\u5269\u4f59\u65f6\u95f4\u5c0f\u4e8e\u7b49\u4e8e0\n        if (leftTime &lt;= 0) {\n            \/\/ \u53d6\u6d88\u5b9a\u65f6\u5668\n            timer.invalidate();\n            \/\/ \u542f\u7528UIDatePicker\u63a7\u4ef6\u548c\u6309\u94ae\n            self.ctimer.isEnabled = true;\n            self.btnStart.isEnabled = true;\n            alertController.message = &quot;\u65f6\u95f4\u5230\uff01&quot;\n        }\n    }\n}<\/code><\/pre>\n<h3>\u95ee\u9898\u89e3\u51b3<\/h3>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5176\u5b9e\u8fd8\u662f\u6709\u4e2a\u5c0fBug\u7684<\/p>\n<ul>\n<li>\n<p>\u95ee\u9898\u63cf\u8ff0\uff1a\u4ee3\u7801\u4e2d\u7ed9\u65f6\u95f4\u63a7\u4ef6\u6dfb\u52a0\u4e86\u4e2a ValueChanged \u4e8b\u4ef6\u76d1\u542c\u54cd\u5e94\uff0c\u76ee\u7684\u662f\u60f3\u6bcf\u6b21\u9009\u62e9\u7684\u65f6\u95f4\u6539\u53d8\u65f6\u90fd\u4f1a\u89e6\u53d1\u6253\u5370\u51fa\u65f6\u95f4\u3002\u4f46\u8fd0\u884c\u4f1a\u53d1\u73b0\uff0c\u7b2c\u4e00\u6b21\u62e8\u52a8\u8868\u76d8\u4e0d\u89e6\u53d1\uff0c\u540e\u9762\u518d\u6539\u53d8\u503c\u624d\u4f1a\u89e6\u53d1\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u8fd9\u4e2a\u662fiOS\u7684bug\uff0c\u6211\u4eec\u628a\u8bbe\u7f6e\u521d\u59cb\u65f6\u95f4\u4ee3\u7801<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">\/\/\u5fc5\u987b\u4e3a 60 \u7684\u6574\u6570\u500d\uff0c\u6bd4\u5982\u8bbe\u7f6e\u4e3a100\uff0c\u503c\u81ea\u52a8\u53d8\u4e3a 60\nself.ctimer.countDownDuration = TimeInterval(leftTime)<\/code><\/pre>\n<p>\u4fee\u6539\u6210<\/p>\n<pre><code class=\"language-swift\">DispatchQueue.main.async {\n    self.ctimer.countDownDuration = TimeInterval(self.leftTime)\n}<\/code><\/pre>\n<blockquote>\n<p>\u5982\u679c\u6211\u4eec\u4e0d\u9700\u8981\u5173\u5fc3\u503c\u6539\u53d8\u4e8b\u4ef6\u7684\u8bdd\uff0c\u76f4\u63a5\u7528\u539f\u6765\u7684\u8d4b\u503c\u65b9\u6cd5\u5373\u53ef<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u5982\u679c\u4f7f\u7528UIDatePicker\u65f6\u5c06\u6a21\u5f0f\u8bbe\u7f6e\u4e3aCountDownTimer\uff0c\u5373\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-589","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=589"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}