let title: String = "您的开奖时间为"
let time: String = "2017-10-23 12:23:18"
let count_down: NSString = "6" as NSString
var countdown = count_down.integerValue
let timeout: String = "开奖时间已超时,请重新获取"
let alertVc = UIAlertController.init(title: nil, message: title + "\n" + time, preferredStyle: UIAlertControllerStyle.alert)
let alertAction0 = UIAlertAction.init(title: "取消", style: .default, handler: { (action) in
})
let alertAction1 = UIAlertAction.init(title: "确定(\(countdown))", style: .default, handler: { (action) in
//确定的操作
})
alertVc.addAction(alertAction1)
alertVc.addAction(alertAction0)
weakSelf?.present(alertVc, animated: true, completion: {
})
if countdown != 0 {
let queue: DispatchQueue = DispatchQueue.global()
let countdownTimer = DispatchSource.makeTimerSource(flags: [], queue: queue)
countdown = countdown + 1
countdownTimer.scheduleRepeating(deadline: .now(), interval: .seconds(1))
countdownTimer.setEventHandler(handler: {
countdown = countdown - 1
if countdown <= 0 {
countdownTimer.cancel()
DispatchQueue.main.async {
alertAction1.setValue("确定(0)", forKey: "title")
alertAction1.setValue(UIColor.hrgb("CCCCCC"), forKey: "titleTextColor")
alertAction1.isEnabled = false
// message
let one: NSString = "\(title)\n\(time)\n" as NSString
let two: NSString = "\(timeout)" as NSString
let message = "\(title)\n\(time)\n\(timeout)"
let alertControllerMessageStr = NSMutableAttributedString(string: message)
alertControllerMessageStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.hrgb("DF503C"), range: NSMakeRange(one.length, two.length))
alertControllerMessageStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13), range: NSMakeRange(one.length, two.length))
alertVc.setValue(alertControllerMessageStr, forKey: "attributedMessage")
}
}else {
DispatchQueue.main.async {
alertAction1.setValue("确定(\(countdown))", forKey: "title")
}
}
})
countdownTimer.resume()
}
Swift alert 倒计时
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 做电商的会遇到秒杀的活动,然后会有倒计时开始抢购的需求,写了个demo来解决这个需求,超级简单的,思路就是获取了当...
- 何谓语文基础知识 语文基础知识,通常主要是指字词书写、拼音、句子排序、病句修改、选词入句。 语文基础知识是小学时最...