[翻译] MZTimerLabel 用作秒表或者倒计时

简介:

MZTimerLabel 用作秒表或者倒计时

https://github.com/mineschan/MZTimerLabel

A handy class for iOS to use UILabel as a countdown timer or stopwatch just like in Apple Clock App.

一个基于UILabel好用的倒计时或者秒表的类。

Purpose(目的

MZTimerLabel is a UILabel subclass, which is a handy way to use UILabel as a countdown timer or stopwatch just like that in Apple Clock App with just 2 lines of code. MZTimerLabel also provides delegate method for you to define the action when the timer finished.

MZTimerLabel是一个UILabel的子类,两行代码就可以显示秒表效果。它提供代理来帮助你处理倒计时结束后的事件处理。

Auther: MineS Chan

Remark: This is my first iOS plugin project on github, please accept my apologize if any bad coding.

Requirements(要求

  • ARC
  • iOS 5.0+

Installations(安装

Manual

  1. Download or clone MZTimerLabel, add MZTimerLabel.h and MZTimerLabel.m souce files into your project.将文件夹MZTimerLabel拖入你的工程当中
  2. #import "MZTimerLabel.h" whereever you need it.引入头文件

CocoaPods

(Unformilar with CocoaPods yet? It's a dependency management tool for iOS and Mac, check it out!)

  1. Add pod 'MZTimerLabel', '~> 0.4.1' to your podfiles

Easy Example(简单示例

To use MZTimerLabel as a stopwatch and counter, you need only 2 lines.

使用MZTimerLabel作为秒表或者定时器,你只需两行代码。

    MZTimerLabel *stopwatch = [[MZTimerLabel alloc] initWithLabel:aUILabel];
    [stopwatch start];

Easy? If you are looking for a timer, things is just similar.

so easy.如果你想使用一个定时器,一样的。

    MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];
    [timer setCountDownTime:60];
    [timer start];

Now the timer will start counting from 60 to 0 ;)

现在,时间就会从60到0了。

Custom Appearance(自定义外观

As MZTimerLabel is a UILabel subclass, you can directly allocate it as a normal UILabel and customizetimeLabel property just like usual.

由于MZTimerLabel是UILabel的子类,你可以直接alloc出来作为一个UILabel然后定制timeLabel属性即可。

    MZTimerLabel *redStopwatch = [[MZTimerLabel alloc] init];
    redStopwatch.frame = CGRectMake(100,50,100,20);
    redStopwatch.timeLabel.font = [UIFont systemFontOfSize:20.0f];
    redStopwatch.timeLabel.textColor = [UIColor redColor];
    [self.view addSubview:redStopwatch];
    [redStopwatch start];

MZTimerLabel uses 00:00:00 (HH:mm:ss) as time format, if you prefer using another format such as including milliseconds.Your can set your time format like below.

MZTimerLabel使用 00:00:00 作为时间格式,如果你想使用毫秒显示。你可以设置成下面的方式。

timerExample4.timeFormat = @"HH:mm:ss SS";

Control the timer(控制定时器

You can start,pause,reset your timer with your custom control, set your control up and call these methods:

你可以开始,暂停,重设你的定时器:

-(void)start;
-(void)pause;
-(void)reset;

And you control the time at the begining or during runtime with these methods

在定时器开始时或者是启动后修改:

-(void)setCountDownTime:(NSTimeInterval)time;
-(void)setStopWatchTime:(NSTimeInterval)time;
-(void)setCountDownToDate:(NSDate*)date;
-(void)addTimeCountedByTime:(NSTimeInterval)timeToAdd;

Timer Finish Handling(倒计时结束后的控制

Usually when you need a timer, you need to deal with it after it finished counting. Following are 2 examples showing how to do it using delegate and block methods.

通常你要定时器,是为了结束后能处理一下时间。下面有两个例子来,一个用代理实现,一个用block来实现。

Delegate

First, set the delegate of the timer label.

首先设置代理

timer.delegate = self;

And then implement MZTimerLabelDelegate protocol in your dedicated class

然后实现 MZTimerLabelDelegate 协议

@interface ViewController : UIViewController<MZTimerLabelDelegate>

Finally, implement the delegate method timerLabel:finshedCountDownTimerWithTimeWithTime:

最后,实现代理方法 timerLabel:finshedCountDownTimerWithTimeWithTime:

 -(void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime{
    //time is up, what should I do master?
 }

Blocks

Block is a very convenient way to handle the callbacks, MZTimerLabel makes your life even easier.

使用block非常便利,MZTimerLabel让你无比轻松。

    MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];
    [timer3 setCountDownTime:60]; 
    [timer startWithEndingBlock:^(NSTimeInterval countTime) {
        //oh my god it's awesome!!
    }];
目录
相关文章
|
负载均衡 网络协议 网络虚拟化
有了 RSTP 为什么还需要 MSTP?
【5月更文挑战第6天】
598 1
有了 RSTP 为什么还需要 MSTP?
|
存储 算法 Unix
操作系统(13)-----文件管理3
操作系统(13)-----文件管理
1241 0
操作系统(13)-----文件管理3
UILabel的文字在左上角显示
UILabel的文字在左上角显示
467 0
UILabel的文字在左上角显示
|
机器学习/深度学习 算法 计算机视觉
【光学】基于matlab实现单缝衍射
【光学】基于matlab实现单缝衍射
|
算法 iOS开发
iOS抽奖转盘下篇:转盘主视图的实现(内含完整Demo)
iOS抽奖转盘下篇:转盘主视图的实现(内含完整Demo)
753 0
iOS抽奖转盘下篇:转盘主视图的实现(内含完整Demo)
不调用- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken的原因
一般 -(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 不调用的原因有如下几种: 1、证书原因 检查证书是否失效 2、设置中没有打开通知的开关 这是最常见的错误,首先要检查的就是这个。
2889 0
|
12天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
11438 122
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
2天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
3358 8
|
1天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
1314 2
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)