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];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值