NSTimer+倒计时功能实现

NSTimer

一、前言,查看官方文档,可以发现NSTimer是Foundation框架下的一个类,它直接继承与NSObject.

二、常用属性

    1、

@property (copy) NSDate *fireDate;

        计时器点燃的时间。返回点燃计时器的最新时间。

    2、

@property (readonly) NSTimeInterval timeInterval;

        用来得到计时器的时间间隔。只读。

    3、

@property NSTimeInterval tolerance NS_AVAILABLE(10_9, 7_0);

        公差。暂时不解释。

三、常用方法

    1

、+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; 

    该方法创建和返回一个新的NSTimer,它被制定的调用object初始化。如果没有设置repeats为YES,那么你必须把实例化的timer用addTimer:forMode:方法添加到循环中。随着时间的流逝,timer开始触发,调用调用方法。

    ti:定时器触发之间的秒数。

    invocation:当计时器点燃时的调用。

    yesOrNo:当是YES时,计时器将会重复直到它无效;如果NO,计时器将会无效当它点燃后。

    2、

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

    该方法和上面的参数一致,也是创建和返回一个新的NSTimer.

    3、

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

    该方法中的参数:

        userInfo:自定义的用户信息针对timer,计时器保持一个强引用对这个info,直到计时器无效,该参数可以为空。

    4、

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

    5、

- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep NS_DESIGNATED_INITIALIZER; 

       该方法中的参数:

            date:计时器开始点燃时的时间。

    6、

- (void)fire;

        该方法使接收的消息被发送到它的目标。可以用这个方法去点燃循环的计时器在不打扰他常规点燃的时刻表。如果计时器不是循环的,它会自动失效。即使它的点燃时刻还没有到来。

    7、

- (void)invalidate;

        停止接收者再次点燃,从循环中去除。它是唯一的方法从循环中移除计时器。 

倒计时功能实现:

  一、直接上代码

  .h文件:

//
//  ViewController.h
//  NSTimerRealiseJishi
//
//  Created by zhanggui on 15/5/20.
//  Copyright (c) 2015年 zhanggui. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
- (IBAction)btnAction:(UIButton *)sender;
@end

  .m文件

//  ViewController.m
//  NSTimerRealiseJishi
//
//  Created by zhanggui on 15/5/20.
//  Copyright (c) 2015年 zhanggui. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSTimer *timer;
    NSInteger totalSeconds;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    totalSeconds = 10;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)changeLabelStr
{
    _timeLabel.text =[NSString stringWithFormat:@"%ld",(long)totalSeconds--];
    if (totalSeconds==0) {
        [timer invalidate];
    }
}
- (IBAction)btnAction:(UIButton *)sender {
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeLabelStr) userInfo:nil repeats:YES];
        [timer fire];
    }
    
}
@end

  主要思想就是:新建一个计时器,然后设置时间间隔和倒计时总时间,然后每次让_timeLabel.text改变就可以了。

原文地址:https://www.cnblogs.com/zhanggui/p/4516353.html