mapanguan

导航

iOS 计时器暂停和开始 防止重复点击

-(IBAction)btnClick{
    [self starTimer];//开始计时
    //[self stopTimer];
}

-(NSTimer*)timer{
    if (!_timer) {
        _timer =[NSTimer scheduledTimerWithTimeInterval:5  target:self  selector:@selector(beginChange) userInfo:nil  repeats:YES];
    }
    return _timer;
}

//暂停定时器(只是暂停,并没有销毁timer)
-(void)pauseTimer{
    [self.timer setFireDate:[NSDate distantFuture]];
}
//继续计时
-(void)continueTimer{
    [self.timer setFireDate:[NSDate distantPast]];
}

//开始计时
-(void)startimer{
    [self.timer fire];
}
//暂停并销毁
-(void)stopTimer{
    [self.timer invalidate];
    self.timer = nil;
}

//开启定时器
-(void)starTimer{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timerStar) object:nil];
    [self performSelector:@selector(timerStar) withObject:nil afterDelay:1];
}

  

posted on 2016-09-08 17:57  mapanguan  阅读(6441)  评论(0编辑  收藏  举报