• 088实现自动倒计时功能


    效果如下:

    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 @end

    ViewController.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     //画面隐藏时,取消计数器
    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     //设置时间显示为1小时5分钟;即65分钟 = 65 * 60(s)
    36     _datePChoice.countDownDuration = 65 * 60;
    37     [self.view addSubview:_datePChoice];
    38     
    39     //使用计数器;设置每隔timeInterval(60秒)循环调用一次
    40     _timer = [NSTimer timerWithTimeInterval:8
    41                                      target:self
    42                                    selector:@selector(timerDidRun:)
    43                                    userInfo:nil
    44                                     repeats:YES]; //这里为了测试;更改timerWithTimeInterval:timeInterval为timerWithTimeInterval:8,即每隔8秒循环调用一次
    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         //日期选择器时间倒计时为0时,取消计数器
    54         if ([_timer isValid]) {
    55             [_timer invalidate];
    56         }
    57     }
    58 }
    59 
    60 @end
  • 相关阅读:
    链家网各城市二手房价格
    mitmproxy 配置
    Python操作APP -- Appium-Python-Client
    Appium连接模拟器
    adb server version (xx) doesn't match this client (xx); killing...
    Appnium 环境搭建
    KeyError: 'xxx does not support field: _id'
    Python执行JS -- PyExecJS库
    Python -- CSV文件读写
    Git的基本使用 -- 分支管理
  • 原文地址:https://www.cnblogs.com/huangjianwu/p/4579237.html
Copyright © 2020-2023  润新知