Introduction to Swift development: CountDown timer (CountDown) and UIDatePicker
Reprinted please declare Source: http://blog.csdn.net/jinnchang/article/details/44619171
Bytes ------------------------------------------------------------------------------------------
Sample Code
Import UIKitclass ViewController: UIViewController {var button: UIButton! Var datePicker: UIDatePicker! Var timer: nstmer! Var remainTime: Double! Override func viewDidLoad () {// initialization button, start countdown button = UIButton. buttonWithType (. system) as UIButton button. frame = CGRectMake (self. view. frame. width/2-200, 50,400, 50) button. setTitle ("Start of Countdown", forState: UIControlState. normal) button. addTarget (self, action: "buttonAction", forControlEvents: UIControlEvents. touchUpInside) // initialize datePicker = UIDatePicker (frame: CGRectMake (0, self. view. frame. height-400, self. view. frame. width, 400) // sets the style, which is currently set as the countdown timer datePicker. datePickerMode = UIDatePickerMode. countDownTimer // set the countdown time datePicker. countDownDuration = 300 self. view. addSubview (button) self. view. addSubview (datePicker)} // click the event button to start countdown func buttonAction () {remainTime = datePicker. countDownDuration button. enabled = false datePicker. enabled = false timer = nstmer. scheduledTimerWithTimeInterval (1, target: self, selector: "countDown", userInfo: nil, repeats: true); timer. fire ()} // trigger func countDown () {if (remainTime <0) {// countDown ends timer. invalidate () button. enabled = true datePicker. enabled = true println ("Countdown to end")} else {println ("\ (remainTime)") remainTime = remainTime-1 datePicker. countDownDuration = remainTime }}}
Bytes ------------------------------------------------------------------------------------------
Result Display
Bytes ------------------------------------------------------------------------------------------
For more information about UIDatePicker, see the previous blog post: getting started with Swift development: UIDatePicker)
Bytes ------------------------------------------------------------------------------------------
Conclusion: GitHub Project address: UIDatePickerSample2
Last article updated: 11:19:52, January 1, March 25, 2015.