[翻译] MZTimerLabel 用作秒表或者倒计时
MZTimerLabel?用作秒表或者倒計(jì)時(shí)
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.
一個(gè)基于UILabel好用的倒計(jì)時(shí)或者秒表的類。
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是一個(gè)UILabel的子類,兩行代碼就可以顯示秒表效果。它提供代理來幫助你處理倒計(jì)時(shí)結(jié)束后的事件處理。
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
CocoaPods
(Unformilar with?CocoaPods?yet? It's a dependency management tool for iOS and Mac, check it out!)
Easy Example(簡(jiǎn)單示例)
To use MZTimerLabel as a stopwatch and counter, you need only?2 lines.
使用MZTimerLabel作為秒表或者定時(shí)器,你只需兩行代碼。
MZTimerLabel *stopwatch = [[MZTimerLabel alloc] initWithLabel:aUILabel];[stopwatch start];Easy? If you are looking for a timer, things is just similar.
so easy.如果你想使用一個(gè)定時(shí)器,一樣的。
MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];[timer setCountDownTime:60];[timer start];Now the timer will start counting from 60 to 0 ;)
現(xiàn)在,時(shí)間就會(huì)從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出來作為一個(gè)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 作為時(shí)間格式,如果你想使用毫秒顯示。你可以設(shè)置成下面的方式。
timerExample4.timeFormat = @"HH:mm:ss SS";
Control the timer(控制定時(shí)器)
You can start,pause,reset your timer with your custom control, set your control up and call these methods:
你可以開始,暫停,重設(shè)你的定時(shí)器:
-(void)start; -(void)pause; -(void)reset;And you control the time at the begining or during runtime with these methods
在定時(shí)器開始時(shí)或者是啟動(dòng)后修改:
-(void)setCountDownTime:(NSTimeInterval)time; -(void)setStopWatchTime:(NSTimeInterval)time; -(void)setCountDownToDate:(NSDate*)date; -(void)addTimeCountedByTime:(NSTimeInterval)timeToAdd;Timer Finish Handling(倒計(jì)時(shí)結(jié)束后的控制)
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.
通常你要定時(shí)器,是為了結(jié)束后能處理一下時(shí)間。下面有兩個(gè)例子來,一個(gè)用代理實(shí)現(xiàn),一個(gè)用block來實(shí)現(xiàn)。
Delegate
First, set the delegate of the timer label.
首先設(shè)置代理
timer.delegate = self;
And then implement?MZTimerLabelDelegate?protocol in your dedicated class
然后實(shí)現(xiàn) MZTimerLabelDelegate 協(xié)議
@interface ViewController : UIViewController<MZTimerLabelDelegate>
Finally, implement the delegate method?timerLabel:finshedCountDownTimerWithTimeWithTime:
最后,實(shí)現(xiàn)代理方法?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!!}];?
?
?
總結(jié)
以上是生活随笔為你收集整理的[翻译] MZTimerLabel 用作秒表或者倒计时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win10 下更改鼠标为左手使用方案
- 下一篇: android 源代码在线查看和索引