088实现自动倒计时功能
生活随笔
收集整理的這篇文章主要介紹了
088实现自动倒计时功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UIDatePicker *datePChoice; 5 @property (strong, nonatomic) NSTimer *timer; 6 7 @endViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)timerDidRun:(NSTimer *)sender; 6 @end 7 8 @implementation ViewController 9 #define timeInterval 60.0 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 [self layoutUI]; 15 } 16 17 - (void)didReceiveMemoryWarning { 18 [super didReceiveMemoryWarning]; 19 // Dispose of any resources that can be recreated. 20 } 21 22 - (void)viewWillDisappear:(BOOL)animated { 23 [super viewWillDisappear:animated]; 24 //畫面隱藏時(shí),取消計(jì)數(shù)器 25 if ([_timer isValid]) { 26 [_timer invalidate]; 27 } 28 } 29 30 - (void)layoutUI { 31 _datePChoice = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; 32 CGPoint newPoint = self.view.center; 33 _datePChoice.center = newPoint; 34 _datePChoice.datePickerMode = UIDatePickerModeCountDownTimer; 35 //設(shè)置時(shí)間顯示為1小時(shí)5分鐘;即65分鐘 = 65 * 60(s) 36 _datePChoice.countDownDuration = 65 * 60; 37 [self.view addSubview:_datePChoice]; 38 39 //使用計(jì)數(shù)器;設(shè)置每隔timeInterval(60秒)循環(huán)調(diào)用一次 40 _timer = [NSTimer timerWithTimeInterval:8 41 target:self 42 selector:@selector(timerDidRun:) 43 userInfo:nil 44 repeats:YES]; //這里為了測試;更改timerWithTimeInterval:timeInterval為timerWithTimeInterval:8,即每隔8秒循環(huán)調(diào)用一次 45 NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 46 [runLoop addTimer:_timer forMode:NSDefaultRunLoopMode]; 47 } 48 49 - (void)timerDidRun:(NSTimer *)sender { 50 if (_datePChoice.countDownDuration > 0.0) { 51 _datePChoice.countDownDuration -= timeInterval; 52 } else { 53 //日期選擇器時(shí)間倒計(jì)時(shí)為0時(shí),取消計(jì)數(shù)器 54 if ([_timer isValid]) { 55 [_timer invalidate]; 56 } 57 } 58 } 59 60 @end?
轉(zhuǎn)載于:https://www.cnblogs.com/huangjianwu/p/4579237.html
總結(jié)
以上是生活随笔為你收集整理的088实现自动倒计时功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 枚举实例分析
- 下一篇: word保存时标题变成黑框(mac版本)