iOS 停止计时器计时

1、

@property (nonatomic, strong)NSTimer *countDownTimer;

2、

//开始计时
-(void)start
{
    //设置倒计时总时长
    secondsCountDown = 15;//秒倒计时
    //设置倒计时显示的时间
    labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];


    [[NSRunLoop currentRunLoop] addTimer:self.countDownTimer forMode:NSRunLoopCommonModes];


}


//停止计时
-(void)stop
{
    if (self.countDownTimer) {
        if ([self.countDownTimer respondsToSelector:@selector(isValid)]) {
            if ([self.countDownTimer isValid]) {
                [self.countDownTimer invalidate];
                self.countDownTimer = nil;
            }
        }
    }
}

-(NSTimer *)countDownTimer
{
    if (!_countDownTimer) {
        //开始倒计时
        _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; //启动倒计时后会每秒钟调用一次方法 timeFireMethod
    }

    return _countDownTimer;
}


-(void)timeFireMethod{
    //倒计时-1
    secondsCountDown--;
    //修改倒计时标签现实内容
    labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];
    //当倒计时到0时,做需要的操作,比如验证码过期不能提交
    if(secondsCountDown==0){
        [_countDownTimer invalidate];
        [labelText removeFromSuperview];


        //引导页按钮被点击,跳转到主界面“UIStoryboard”
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];// Main是Main.sroryboard的的前缀名,Main是固定的
        UITabBarController * MainVc = [story instantiateViewControllerWithIdentifier:@"tabBar"];//在Main.sroryboard上设置“tabBar”,相当与给Main.sroryboard打标签,
        UIWindow *getWindow = [UIApplication sharedApplication].keyWindow;
// UIWindow *getWindow = [[[UIApplication sharedApplication] delegate] window];
        getWindow.rootViewController = MainVc;
        MainVc.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
        MainVc.view.transform = CGAffineTransformIdentity;

        //--------------当跳转页面时,添加动画效果
// [UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.5];
// [UIView commitAnimations];
        //----------------------

        [[UIApplication sharedApplication] setStatusBarHidden:NO];
    }
}

你可能感兴趣的:(iOS 停止计时器计时)