ios - 两个日期之间的计时器倒计时

标签 ios iphone swift timer

我想在两个日期之间集成计时器,一个是选定日期,另一个是按钮标题上的当前日期,就像任何其他倒计时应用一样。

func countDownDate() {
    var calendar = Calendar.current
    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: self.startDate, to: self.currentDate as Date)
    let countdown = "Days \(String(describing:diffDateComponents.day!)),  Hours: \(String(describing: diffDateComponents.hour!)), Minutes: \(String(describing: diffDateComponents.minute!)), Seconds: \(String(describing: diffDateComponents.second!))"
    print("countdown",countdown)
    var dayText = String(describing: diffDateComponents.day!) + "d "
    var hourText = String(describing: diffDateComponents.hour!) + "h "
    self.button_CreateBid.setTitle(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s", for: .normal)
}


let when = DispatchTime.now() + 0.1 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
    Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ServiceDetailViewController.countDownDate), userInfo: nil, repeats: true)
}

最佳答案

我已经检查了您的代码及其在我的系统上的运行情况。您可以在稍作改动后运行它。下面是我的代码。

@IBOutlet weak var btnTimeLabel : UIButton!

把这段代码放在viewDidLoad

let when = DispatchTime.now() + 0.1 // change 2 to desired number of seconds
        DispatchQueue.main.asyncAfter(deadline: when) {
            Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.countDownDate), userInfo: nil, repeats: true)
        }

我已经在您的函数中设置了下一个日期和当前日期。

 @objc func countDownDate() {

        let dateFormatter = DateFormatter()
               dateFormatter.dateFormat = "dd-mm-yyyy" //Your date format
               dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
               let futuredate = dateFormatter.date(from: "22-02-2018") //according to date format your date string
        var calendar = Calendar.current
        let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: futuredate!, to: Date())
        let countdown = "Days \(String(describing:diffDateComponents.day!)),  Hours: \(String(describing: diffDateComponents.hour!)), Minutes: \(String(describing: diffDateComponents.minute!)), Seconds: \(String(describing: diffDateComponents.second!))"
        print("countdown",countdown)
        var dayText = String(describing: diffDateComponents.day!) + "d "
        var hourText = String(describing: diffDateComponents.hour!) + "h "
        btnTimeLabel.setTitle(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s", for: .normal)
        //print(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s")
    }

关于ios - 两个日期之间的计时器倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48905210/

相关文章:

ios - 以编程方式在 swift ios 中创建复选框按钮

objective-c - 使用 Shift 键扩展 NSCollectionView 中的选择

ios - 将 subview 的子类添加到 UIView 的子类

ios - 在react native中,如何将视频组件设置为页面背景?

ios - 关闭 View Controller 自定义动画

ios - iOS获取国家代码不起作用

iphone - 有没有办法从 iOS 5.1 降级到 iOS 5.0?

iphone - iPhone 上 OpenAL 声音缓冲区的最大数量

objective-c - 在不闪烁的情况下更新 MKannotation 图像

ios - NSNotificationCenter 不会在 Swift 中发送消息