美文网首页iOS小项目
倒计时按钮-HTCountDownButton

倒计时按钮-HTCountDownButton

作者: 阿洋12138 | 来源:发表于2017-11-07 14:43 被阅读7次

    我项目中经常会遇到一个情况,在tableview的cell中会有倒计时的显示,但是cell有复用的机制,所以做倒计时有点麻烦,我自己做了这么个轮子,方便使用。
    github下载地址: https://github.com/KingXcode/HTCountDownButton

    HTCountDownButton继承与UIButton

    按钮的内容是居左显示,与正常的button居中显示不一样,
    如果需要正常请设置contentHorizontalAlignment属性为居中显示
    
    结束的时间戳  如果是服务器的时间戳需要除以1000
    只要设定好结束时间即可开始进行倒计时
    @property (nonatomic,assign)NSTimeInterval endTime;
    
    倒计时回调。大约每0.1秒左右回调一次
    time 是剩余的秒数
    info 字典对time进行了分解  需要就用 不需要就不要理这个参数
    一下是info中具体包含的数据
    NSDictionary *info = @{
                            @"hours"             :@(hours),
                            @"minutes"         :@(minutes),
                            @"seconds"        :@(seconds),
                            @"millisecond"   :@(millisecond)
                            };
    
    @property (nonatomic, copy) void(^countDownChanging)(HTCountDownButton *,NSTimeInterval time,NSDictionary *info);
    
    倒计时结束的时候调用
    @property (nonatomic, copy) void(^countDownEnd)(HTCountDownButton *);
    
    实现原理:
    -(void)setEndTime:(NSTimeInterval)endTime;
    该方法给_endTime变量赋值,并且会对比当前时间的时间戳与_endTime时间戳计算秒数并且给 _limit赋值
    
    -(void)setLimit:(NSTimeInterval)limit;
    此方法中会判断定时器是否存在,不存在则创建启动定时器开始倒计时
    
    此外还有一个label的分类。实现原理与button的原理一样。
    并且这个label的分类也可用在button的的子控件label中。

    相关文章

      网友评论

        本文标题:倒计时按钮-HTCountDownButton

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