MZTimerLabel——倒計(jì)時(shí)標(biāo)簽組件

MZTimerLabel

image

目的

MZTimerLabel 是 UILabel 的子類,它提供了使用 UILabel 作為倒計(jì)時(shí)器或者秒表(類似于Apple 的時(shí)鐘App)的便捷方法,而且最少只需要2行代碼。MZTimerLabel 也提供倒計(jì)時(shí)結(jié)束時(shí)的協(xié)議方法以供調(diào)用。

簡單示例

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

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

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

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

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

自定義外觀

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

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.

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; // 重設(shè)

Getter and Setters

You may control the time value and behaviours at the begining or during runtime with these properties and methods

通過以下這些方法,你可以在倒計(jì)時(shí)一開始或者進(jìn)行中控制時(shí)間值和行為。

@property (assign) BOOL shouldCountBeyondHHLimit;   //see example #12
@property (assign) BOOL resetTimerAfterFinish;      //see example #7

-(void)setCountDownTime:(NSTimeInterval)time;
-(void)setStopWatchTime:(NSTimeInterval)time;
-(void)setCountDownToDate:(NSDate*)date;
-(void)addTimeCountedByTime:(NSTimeInterval)timeToAdd; //see example #10, #11

And if you want to have information of the timer, here is how.

@property (assign,readonly) BOOL counting;  //see example #4-7

- (NSTimeInterval)getTimeCounted;    //see example #3
- (NSTimeInterval)getTimeRemaining;  //see example #3
- (NSTimeInterval)getCountDownTime;  

Timer Finish Handling

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

代理方法

First, set the delegate of the timer label.

timer.delegate = self;

And then implement MZTimerLabelDelegate protocol in your dedicated class

@interface ViewController : UIViewController<MZTimerLabelDelegate>

Finally, implement the delegate method 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.

MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];
[timer3 setCountDownTime:60]; 
[timer startWithEndingBlock:^(NSTimeInterval countTime) {
   //oh my gosh, it's awesome!!
}];

Or set it seperately

[timer3 setCountDownTime:60]; 
timer.endedBlock = ^(NSTimeInterval countTime) {
   //oh my gosh, it's awesome!!
};
[timer start];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容