iOS倒计时毫秒-仿唯品会倒计时

demo.gif

直接上代码

- (void)countTime{
    AFWeakSelf;
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] ;
    double currentTime = [self.model.promote_end_date doubleValue] - interval;
    __block float timeout= currentTime; //倒计时时间
    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_walltime(NULL, 0),0.1*NSEC_PER_SEC, 0); // 每100毫秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束,关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                // 倒计时结束,关闭处理

            });
        }else{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] ;
                double currentTime = [self.model.promote_end_date doubleValue] - interval;

                int currentHour  = currentTime / 3600;
                int currentMinute = (currentTime - currentHour*3600) / 60;
                int currentSeconds = currentTime - currentHour*3600 -currentMinute*60;
                int currentMsec = currentTime*1000 - currentHour*3600*1000 - currentMinute*60*1000 - currentSeconds*1000;
                
                weakSelf.hourLabelA.text = [NSString stringWithFormat:@"%d",currentHour/10];
                weakSelf.hourLabelB.text = [NSString stringWithFormat:@"%d",(currentHour%10)];
                weakSelf.minuteLabelA.text = [NSString stringWithFormat:@"%d",currentMinute/10];
                weakSelf.minuteLabelB.text = [NSString stringWithFormat:@"%d",currentMinute%10];
                weakSelf.secondLabelA.text = [NSString stringWithFormat:@"%d",currentSeconds/10];
                weakSelf.secondLabelB.text = [NSString stringWithFormat:@"%d",currentSeconds%10];
                weakSelf.msecLabel.text = [NSString stringWithFormat:@"%d",currentMsec/100%1000];
                
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    self.myTimer = _timer;
}

GCD定时器非常耗性能,耗内存,pop这个控制器的时候发现没有释放,
在控制器即将消失

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

       // 取出对应的cell,取消cell上的定时器
        NSIndexPath *indexP = [NSIndexPath indexPathForRow:0 inSection:0];
        MFMainCityDetailGrabCell *cell = [self.detailTableView cellForRowAtIndexPath:indexP];
        if (cell.myTimer) {
            dispatch_source_cancel(cell.myTimer);
            cell.myTimer = nil;
        }
}

求助,各位大神,有没有什么方法释放掉这个控制器????

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

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,209评论 30 471
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,179评论 25 708
  • 每天事情都特别多,时间特别赶,今天爬楼听了芳老师的成交技巧,茅塞顿开的感觉,四种方法中自己平时做的用的最多的就是开...
    FAB默默阅读 202评论 0 0
  • 梦想建立在现实的基础上,今天我站在这里,和你们说,我的梦想是要当美国总统,你们信吗?可能所有人都不相信吧。倘若我...
    一个有点理想的文艺青年阅读 144评论 0 0
  • 2017年2月21日,已放弃外国国籍成为中国公民的中国科学院外籍院士杨振宁、姚期智两位教授日前正式转为中国科学院院...
    ACE小飞阅读 6,243评论 208 105