博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
倒计时按钮,根据时间单位转化
阅读量:5975 次
发布时间:2019-06-20

本文共 3264 字,大约阅读时间需要 10 分钟。

////  CountdownButton.h//  Standardization////  Created by YuFly on 2017/10/16.//  Copyright © 2017年 strong. All rights reserved.//#import 
@class CountdownButton;typedef void(^Completion)(CountdownButton *countdownButton);@interface CountdownButton : UIButton/** 倒计时 @param startTime 倒计时时间 @param timeUnit 时间单位 @param completion 结束回调 */- (void)countDownButtonWithStartTime:(NSInteger)startTime timeUnit:(NSString *)timeUnit endTitle:(NSString *)endTitle completion:(Completion)completion;@end
////  CountdownButton.m//  Standardization////  Created by YuFly on 2017/10/16.//  Copyright © 2017年 strong. All rights reserved.//#import "CountdownButton.h"@implementation CountdownButton- (void)countDownButtonWithStartTime:(NSInteger)startTime                            timeUnit:(NSString *)timeUnit                            endTitle:(NSString *)endTitle                          completion:(Completion)completion{    __unsafe_unretained CountdownButton *ws=self;    NSInteger beginTime = startTime;    if([[timeUnit lowercaseString] isEqualToString:@"s"])        beginTime = startTime;    else if([[timeUnit lowercaseString] isEqualToString:@"min"])        beginTime = startTime*60;    else if([[timeUnit lowercaseString] isEqualToString:@"h"])        beginTime = startTime*60*60;    __block NSInteger remainTime = beginTime;    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_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);    dispatch_source_set_event_handler(timer, ^{        if (remainTime <= 0) { // 倒计时结束            dispatch_source_cancel(timer);            dispatch_async(dispatch_get_main_queue(), ^{                 [ws setTitle:[NSString stringWithFormat:@"%@",endTitle] forState:UIControlStateDisabled];                ws.enabled = YES;                completion(ws);            });        } else {            NSString *timeStr = [ws getMMSSFromSS:[NSString stringWithFormat:@"%ld", remainTime] timeUnit:timeUnit];            dispatch_async(dispatch_get_main_queue(), ^{                [ws setTitle:[NSString stringWithFormat:@"允许%@倒计时%@",endTitle,timeStr] forState:UIControlStateDisabled];                ws.enabled = NO;            });            remainTime--;        }    });    dispatch_resume(timer);}-(NSString *)getMMSSFromSS:(NSString *)timeString timeUnit:(NSString *)timeUnit{        NSInteger seconds = [timeString integerValue];    NSString *hour = [NSString stringWithFormat:@"%02ld",seconds/3600];    NSString *minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];    NSString *second = [NSString stringWithFormat:@"%02ld",seconds%60];    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",hour,minute,second];        if([[timeUnit lowercaseString] isEqualToString:@"s"])    {        format_time= timeUnit;    }    else if([[timeUnit lowercaseString] isEqualToString:@"min"])    {        format_time = [NSString stringWithFormat:@"%@:%@",minute,second];    }    return format_time;}@end

 

转载于:https://www.cnblogs.com/YuFly-lyx/p/7681312.html

你可能感兴趣的文章
厚积薄发,看腾讯云如何快速从IPv4向IPv6演进?
查看>>
GitLab公布关于开发者趋势的问卷调查结果
查看>>
准备好了?测试人员迟早会被要求测试包含区块链技术的解决方案
查看>>
IBM发表论文:可能已找到处理量子计算退相干的方法
查看>>
WCF与ASP.NET Core性能比较
查看>>
访谈Stuart Davidson:Skyscanner的持续交付推广
查看>>
一地鸡毛 OR 绝地反击,2019年区块链发展指南
查看>>
QLoo推出用于现有服务的GraphQL接口
查看>>
从Java到Spring为何独得青睐Spring Summit 2017不可不知的那些事儿
查看>>
取代Python多进程!伯克利开源分布式框架Ray
查看>>
百度举办第七届技术开放日,揭秘春晚红包技术支撑
查看>>
Oracle宣布提供新的Java支持价格体系
查看>>
广发银行运维实践分享:Docker适配传统运维那些事
查看>>
EF Core数据库Provider一览
查看>>
Kafka团队修改KSQL开源许可,怒怼云厂商
查看>>
命令行神器 Click 简明笔记
查看>>
RocketMQ Apache顶级项目之路
查看>>
苹果在GitHub上正式开源iOS内核源码
查看>>
测试人员面临的测试挑战和必备技能
查看>>
使用Flutter之后,我们的CPU占用率降了50%
查看>>