美文网首页集思广益复制粘贴IOS
iOS倒计时毫秒-仿唯品会倒计时

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

作者: 船长_ | 来源:发表于2016-09-01 18:18 被阅读1227次
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;
        }
}

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

相关文章

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

    直接上代码 GCD定时器非常耗性能,耗内存,pop这个控制器的时候发现没有释放,在控制器即将消失 求助,各位大神,...

  • iOS高仿国美、二次元应用、点赞喷射动画、电影筛选页等源码

    iOS精选源码 高仿国美 iOS 弹幕库 仿直播321倒计时动画 高仿网易二次元GACHA iOS 中间带凸起旋转...

  • 连马云都想见的人:唯品会90后董事长兼CEO沈亚

    唯品会12.8周年庆倒计时7天 连马云都想见的人:唯品会90后董事长兼CEO沈亚 在电商...

  • 倒计时

    ios怎么在cell上添加倒计时 iOS中 简单易懂的秒杀倒计时/倒计时 iOS开发-三种倒计时的写法 iOS实现...

  • iOS 毫秒倒计时列表

    近段的项目有个毫秒倒计时的需求,在网上找了好久,都没有找到明确的Demo或者毫秒倒计时的知识点,所以就借鉴了一些前...

  • iOS毫秒倒计时的实现

    app开发中,当展示限时优惠的某些商品时,往往会加一个倒计时,提示用户该商品限时优惠所剩的时间,。那对于开发者来说...

  • 关于倒计时的几种

    Via From Net iOS倒计时的探究与选择 只用一个倒计时,改变数据源实现 一个倒计时,单例实现

  • 高考倒计时

    闺女已经进入高考倒计时。记忆中,中考倒计时仿佛就在昨天,而今已经迈进高三的门槛,高考进入倒计时,唯觉时间就在转瞬间...

  • iOS快速实现一个保存记录的倒计时按钮

    iOS开发中在登录、注册、找回密码等页面经常需要实现倒计时按钮,但是很多情况下用户点击倒计时按钮开始倒计时之后,再...

  • 唯品会体验报告

    一、这款产品是什么? 产品介绍 1、名称唯品会IOS版 2、版本v5.34 二、产品定位 产品目标用户 唯品会的目...

网友评论

  • 那么你就是我的:没关闭timer,timer在不使用的时候需要关闭并且置为nil,【timer invalidate】
  • ISwiftUI:要创建成强引用的属性对象,然后在控制器将要消失前释放
  • iCotton:这种情况CADisplayLink应该更适合
  • 混不吝丶:getMain queue时释放Timer
  • 不上火喝纯净水:没看出来什么问题,把self.model 也改成weakself试试
  • Mr_Victory:你在dealloc方法中打印下看控制器是否被释放。如果没有,那可能是timer没被销毁。
  • 郑州程序员王一:很好,坐等大神回复

本文标题:iOS倒计时毫秒-仿唯品会倒计时

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