Introduction to Swift development: CountDown timer (CountDown) and UIDatePicker

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.