[翻译] 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
CocoaPods
(Unformilar with?CocoaPods?yet? It's a dependency management tool for iOS and Mac, check it out!)
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!!}];?
?
?
總結
以上是生活随笔為你收集整理的[翻译] MZTimerLabel 用作秒表或者倒计时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win10 下更改鼠标为左手使用方案
- 下一篇: 原创:秋收起义部队原本要南下会合南昌起义