Swift - 使用UIDatePicker實(shí)現(xiàn)倒計(jì)時(shí)功能

import UIKit
class ViewController: UIViewController {
var  ctimer:UIDatePicker!
var starBtn :UIButton!
var letfTime:Int = 180
var alertView : UIAlertView!
var timer :NSTimer!
override func viewDidLoad() {
    super.viewDidLoad()
 var SCREEN_WIDTH  = self.view.frame.width
  var SCREEN_HIGHT = self.view.frame.height
    ctimer = UIDatePicker(frame:CGRectMake(20,120,SCREEN_WIDTH-40,200))
    self.ctimer.datePickerMode = UIDatePickerMode.CountDownTimer
    //必須為 60 的整數(shù)倍,比如設(shè)置為100,值自動(dòng)變?yōu)?60
    self.ctimer.countDownDuration = NSTimeInterval (letfTime)
    ctimer.addTarget(self, action: "ChangeValue", forControlEvents: UIControlEvents.ValueChanged)
    self.view.addSubview(ctimer)
    
    starBtn = UIButton (type: UIButtonType.System)
    starBtn.frame = CGRect(x: 100, y: 400, width: SCREEN_WIDTH-200, height: 100)
    starBtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
    starBtn.setTitleColor(UIColor.greenColor(), forState: UIControlState.Disabled)
    starBtn.setTitle("開始", forState: UIControlState.Normal)
    starBtn.setTitle("倒計(jì)時(shí)中", forState: UIControlState.Disabled)
    starBtn.clipsToBounds = true
    starBtn.layer.cornerRadius = 5
    starBtn.addTarget(self, action: "BtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(starBtn)
    
    

}
/**
 開始倒計(jì)時(shí)按鈕點(diǎn)擊
 */
func BtnClick(sender:UIButton){
 
    self.starBtn.enabled = false
    //獲該取倒計(jì)時(shí)器的剩余時(shí)間
    letfTime = Int(self.ctimer.countDownDuration)
    // 禁用UIDatePicker控件和按鈕
    self.ctimer.enabled = false
    // 創(chuàng)建一個(gè)UIAlertView對(duì)象(警告框),并確認(rèn),倒計(jì)時(shí)開始
    alertView = UIAlertView()
    alertView.title = "倒計(jì)時(shí)開始"
    alertView.message = "倒計(jì)時(shí)開始還有\(zhòng)(letfTime)秒......"
    alertView.addButtonWithTitle("確定")
    //顯示UIAlerView組件
    alertView.show()
    // 啟用計(jì)時(shí)器,控制每秒執(zhí)行一次tickDown方法
    timer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(1), target: self, selector: Selector("tickDown"), userInfo: nil, repeats: true)
}

func tickDown(){
    
    alertView.message = "倒計(jì)時(shí)開始,還有\(zhòng)(letfTime)秒!"
    //將剩余時(shí)間減1秒
    letfTime -= 1
    //修改UIDatePicker的剩余時(shí)間
    //self.ctimer.countDownDuration = NSTimeInterval(letfTime)
    dispatch_async(dispatch_get_main_queue(), {
      self.ctimer.countDownDuration = NSTimeInterval(self.letfTime);
    })
    print(letfTime)
    //如果剩余時(shí)間小于等于0
    if(letfTime <= 0){
    //取消定時(shí)器
        
        timer.invalidate()
    //啟用UIDatePicker控件和按鈕
        ctimer.enabled = true
        starBtn.enabled = true
        alertView.message = "時(shí)間到"
    
    }
    
}

func ChangeValue(){
    print("您選擇倒計(jì)時(shí)為:\(self.ctimer.countDownDuration)")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容