美文网首页
iOS 倒计时实现

iOS 倒计时实现

作者: Lskyu | 来源:发表于2018-11-11 17:39 被阅读0次

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

    #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
    

    相关文章

      网友评论

          本文标题:iOS 倒计时实现

          本文链接:https://www.haomeiwen.com/subject/eqnxfqtx.html