iOS 倒计时实现

方法一:(切换后台会暂停)

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong) UIButton *adSkip;
@property(nonatomic,strong) NSTimer *clockTimer;
@property(nonatomic,assign) NSInteger seconds;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _seconds = 10;
    _adSkip = [UIButton buttonWithType:UIButtonTypeCustom];
    [_adSkip setTitle:@"10" forState:UIControlStateNormal];
    
    _adSkip.titleLabel.font = [UIFont systemFontOfSize:18];
    [_adSkip setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _adSkip.backgroundColor = [UIColor blueColor];
    [_adSkip setFrame:CGRectMake(20, 20, 100, 100)];
    [self.view addSubview:_adSkip];
    
    _clockTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(oneSecondPass) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_clockTimer forMode:NSDefaultRunLoopMode];
}

-(void)oneSecondPass{
    if (_seconds > 0) {
        _seconds =_seconds-1;
        [_adSkip setTitle:[NSString stringWithFormat:@"%ld",_seconds] forState:UIControlStateNormal];
    } else{
        [_clockTimer invalidate];
        _clockTimer = nil;
    }
}

@end

方法二(切换后台会暂停)

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong) UIButton *adSkip;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _adSkip = [UIButton buttonWithType:UIButtonTypeCustom];
    _adSkip.titleLabel.font = [UIFont systemFontOfSize:18];
    [_adSkip setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _adSkip.backgroundColor = [UIColor blueColor];
    [_adSkip setFrame:CGRectMake(20, 20, 100, 100)];
    [self.view addSubview:_adSkip];
    
    [self startTime];
}

-(void)startTime{
    __block int seconds = 15;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (seconds <=0) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.adSkip setTitle:@"0" forState:UIControlStateNormal];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1.0];
                [self.adSkip setTitle:[NSString stringWithFormat:@"%d",seconds] forState:UIControlStateNormal];
                [UIView commitAnimations];
                
            });
            seconds--;
        }
    });
    dispatch_resume(timer);
}

方法三:(切换后台不会暂停)

- (void)viewDidLoad {
    [super viewDidLoad];
    _adSkip = [UIButton buttonWithType:UIButtonTypeCustom];
    _adSkip.titleLabel.font = [UIFont systemFontOfSize:18];
    [_adSkip setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _adSkip.backgroundColor = [UIColor blueColor];
    [_adSkip setFrame:CGRectMake(20, 20, 100, 100)];
    [self.view addSubview:_adSkip];
    [self.adSkip setTitle:@"15" forState:UIControlStateNormal];
    
    [self startTime];
}

-(void)startTime{
    int seconds = 15;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    
    NSDate *endTime = [NSDate dateWithTimeIntervalSinceNow:seconds];
    dispatch_source_set_event_handler(timer, ^{
        int interval = [endTime timeIntervalSinceNow];
        if (interval <= 0) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.adSkip setTitle:@"0" forState:UIControlStateNormal];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1.0];
                [self.adSkip setTitle:[NSString stringWithFormat:@"%d",interval] forState:UIControlStateNormal];
                [UIView commitAnimations];
                
            });
        }
    });
    dispatch_resume(timer);
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容