mineschan / mztimerlabel Goto Github PK
View Code? Open in Web Editor NEWA handy class for iOS to use UILabel as a countdown timer or stopwatch just like in Apple Clock App.
License: MIT License
A handy class for iOS to use UILabel as a countdown timer or stopwatch just like in Apple Clock App.
License: MIT License
The time of label is not correctly showed while tableview reuses cell. In my subclass cell, I set the MZTimerLabel
in a timeStamp
property setter method,
- (void)setTimeStamp:(NSUInteger)timeStamp
{
_timeStamp = timeStamp;
self.countLabel.timeFormat = @"mm:ss:SS";
if (_timeStamp > 3600) {
self.countLabel.timeFormat = @"HH:mm:ss:SS";
}
if (_timeStamp && self.countLabel.counting) {
[self.countLabel reset];
[self.countLabel setCountDownTime:self.countLabel.getTimeRemaining];
} else {
[self.countLabel setCountDownTime:_timeStamp];
}
WS(weakSelf);
[self.countLabel startWithEndingBlock:^(NSTimeInterval countTime) {
NSLog(@"%zd",countTime);
weakSelf.countLabel.text = @"Finished";
}];
}
It seems work, however, cell's self.countLabel.getTimeRemaining
is not correctly.
if (_timeStamp && self.countLabel.counting) {
[self.countLabel reset];
[self.countLabel setCountDownTime:self.countLabel.getTimeRemaining];
} else {
[self.countLabel setCountDownTime:_timeStamp];
}
The main logic is while counting
is YES
, assign the remaining time to the label instead of the original time data.
//Creating a timer
_timerView = [[MZTimerLabel alloc] initWithLabel:_timerLabel andTimerType:MZTimerLabelTypeTimer];
_timerView.timeFormat = @"HH:mm:ss,SS";
_timerView.countDownTime = _currentTimer.timeInterval; // timeInterval is for example 14
_timerView.delegate = self;
[_timerView start];
timerLabel:(MZTimerLabel *)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime
is called when the label shows 00:00:00,99 so it doesn't count to zero.
This project is great, but why the first second is not counted correctly (the initial value is not fully counted - it's not completely marks the first second) and why the end count 00:00:00 instead passing from 00:00:01 to the delegate? Call the delegate after 00:00:00 is to count a second more.
If you check clock app in iOS the countdown and generally every countdown works differently from MZT.
Is possible correct that?
Thanks
Hi, I just found this project and I really like it. I have a timer in every UITableViewCell that I create and after a countdown of 24 hours, I want to delete the cell whose timer went to <= 0. Each timer will have a different time. I understand we can use the delegate method timerLabel(timerLabel: MZTimerLabel!, finshedCountDownTimerWithTime countTime: NSTimeInterval)
to do things when the timer completes but is there a way to pass in the index of the UITableViewCell or the actual UITableViewCell to this delegate
method so that I can delete the cell whenever that timer goes to 0? If you could help me, that would be great. Thanks!
Hi,
There is a logic flaw:
if (self.timerType == MZTimerLabelTypeStopWatch && timeUserValue > 0) { startCountDate = [startCountDate dateByAddingTimeInterval:(timeUserValue<0)?0:-timeUserValue]; // no point at all to check for timeUserValue<0, because there is timeUserValue > 0 in outer IF }
i'd like to insert label with timer in nav bar viz. in rightBarButtonItem. There is the code:
UILabel _label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 40.0, 100.0)];
lblTimer = [[MZTimerLabel alloc] initWithLabel:label andTimerType:MZTimerLabelTypeTimer];
[lblTimer setCountDownTime:21_60];
lblTimer.bounds = CGRectOffset(label.bounds, 0, 0);
lblTimer.font = FONT_B(18.0f);
lblTimer.textColor = [UIColor whiteColor];
[lblTimer sizeToFit];
lblTimer.delegate = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:lblTimer];
[lblTimer start];
The result: in nav bar appears timer label with 00:00:00 (NOT 00:21:60) and timer is not starting... What am i do wrong? thanks for answering. With last holidays!=)
Awesome project!
Is there any way to implement the count down for the stopwatch type?
如果我像這樣指定倒計時的格式:
timeLabel.timeFormat = @"MM-dd HH:mm:ss";
那麼將會得到一個錯誤的結果:MM 和 dd 表示的值會多 1。
比如最後 3s 的倒計時應該是 00-00 00:00:03,結果卻是 01-01 00:00:03
Hi,
I am using a custom timeFormat (HH:mm).
So there is not much happening for at least 60 seconds. So I was thinking, it would be cool if the colon could be blinking every second. So that it feels more like a digital clock.
Is this something that would make sense incorporating?
Looking forward to your feedback.
Hello,
I want to use this wonderful timer to display seconds, minutes, hours and days remaining.
Unfortunately, the day value is incorrect (there is always an extra one day displayed).
Is there a workaround to get this working properly ?
Here is my code :
@IBOutlet weak var timer1: MZTimerLabel!
let interval = NSTimeInterval(8) // 8 seconds
timer1.setCountDownTime(interval)
timer1.timeFormat = "dd-HH-mm-ss"
timer1.start()
// displays 01-00-00-08 instead of 00-00-00-08
Thank you !
For Countdown Timer logic in updateLabel
method you have:
-(void)updateLabel{
// ...
[self pause];
timeToShow = [date1970 dateByAddingTimeInterval:0];
pausedTime = nil;
// ...
}
Pause method, however, does self.pausedTime = [NSDate date];
Is setting pausedTime
to nil
intentional?
I set a timer label in one view, then back to the previous view using [self dismissViewControllerAnimated:YES completion:nil];
. But the timer didn't stop because the delegate method was called when the timer was end.
First of all thanks for this great pod/control
I have encountered an issue and would like to inform you. So I have MZTimerLabel in my view, actually it is a normal UILabel in storyboard file but in the code I used the initWithLabel: method to create a MZTimerLabel with an existing UILabel, everything works fine.
But then I wanted to include a pause screen in my viewController in my pauseTimer; function I called [timerLabel pause]; and did some other work. but when I resume the timer in another function of mine by calling [timerLabel start]; it would not resume from that time, rather it would resume from some random time which is 3-15 seconds added to the current time or just go to zero by its own.
I am waiting for your reply. Thanks
Hi and thanks for your great classes !
I was wondering : could you add a method to add a block to execute when a countDown timer ends independently of starting it ? I mean we could do
timerLabel.endingBlock = ^(NSTimeIntervall time){...} ;
Thanks
Looking at the implementation of MZTimerLabel, it looks like the NSTimers are never invalidated when the MZTimerLabel instance is removed from the current view/window. For repeating timers, it's generally best practice to call invalidate
when they are not being used anymore (e.g. the view is not shown) since they retain the target they are assigned to. This means that MZTimerLabel never has its dealloc
method called.
From the [Apple docs]:
Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.
Looking at this [SO question], it looks like the best place to do this is in the removeFromSuperview
method of the UIView.
Can you add a option for getting the counted time in milliseconds
Putting an AlertViewController within this method will make the AlertViewController carry over to the next viewcontroller. I am doing a quiz type app using your timer, and cannot figure out how to invalidate the timer before it goes to the next view. I have tried pause, setting the delegate to nil, timer to nil, etc.
Hi and thanks for your great classes !
I was wondering : could you add a method to get the remaining time for a countDown timer?
Thanks
We need Swift Version
As apple release iOS8 and the way to work with size classes, does anyone have issue on implementing MZTimerlabel using iOS8 + auto layout? If so, any suggestions?
Hi, I'm new to using MZTimerLabel, and it's an awesome little utility. I'm not sure if this is an isolated issue with XCode7 and iOS9 or not:
I'm using a countdown timer, with the following settings:
labelTimer = [[MZTimerLabel alloc] initWithLabel:lblLine8 andTimerType:MZTimerLabelTypeTimer];
[labelTimer setCountDownToDate:endDate];
labelTimer.timeFormat = @"hh:mm:ss";
labelTimer.resetTimerAfterFinish = NO;
labelTimer.delegate = self;
[labelTimer start];
Here's the issue:
When the endDate is over 1 hour ahead, then the labelTimer shows as e.g: 01:45:22, and can be seen counting down.
Then when the labelTimer drops from 01:00:00, it displays as 12:59:59 and shows counting down (but I would expect to see 00:59:59 and counting down).
If I manually set the countdown to less than one hour using: -(void)setCountDownTime:(NSTimeInterval)time, when the time format is: hh:mm:ss, then the label still shows as: 12:XX:XX
As a workaround, I've had to modify my code to check the value of the label timer using "- (NSTimeInterval)getTimeRemaining", and when it's > 1h, I set labelTimer.timeFormat = @"hh:mm:ss";
And when it's < 1h, I set labelTimer.timeFormat = @"mm:ss";
This sort of gets the job done, but it's not ideal.
Am I doing something wrong? I've checked over all the docs and the MZTimerLabel.h file to make sure I'm not missing anything. Maybe this is isolated to XCode7 / iOS9.
Thanks,
Chris
I'm starting a countdown timer with a click on button in my app and set time to 10 seconds. After the countdown is finished, I start another countdown timer with 30 seconds. I use the delegate method -(void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime
to start second timer.
However, if I close the app immediatly after the first countdown timer started, the delegate method is never called and the second timer never starts. Only after I open the app.
Any idea how to start new timer while app is in background?
Following Cocoa convention, I suggest changing [stopwatch start] to [stopwatch resume].
(and maybe add a pause feature as well).
When you create instance of MZTimerLabel and it is not added as subview (You just create instance and then set timeLabel), removeFromSuperview is not fired > timer is not invalidated and instance stays in memory.
I experience there is a bug in countdown timer, if the count down date time is more than 24 hours away from current time. For example if you pass, [timerLabel setCountDownTime:3600*26] . It will start counting down for 2 hours.
Hello,
I try to add your label inside a tableView, but it doesn't work, it keeps showing in all cells and strange behavior.
遇到一个需求,显示倒计时,用了你的,时间部分的设置需要不同颜色字体,但是没有提供相关接口
Hey there, great library! I want to request a feature that once the count down is finished, it will restore the label's text when it starts counting. Currently I can only use the delegate to restore the text.
Hi,
Could you please update POD with the latest fix?
Thanks,
H.
Hello MZTimerLabel help me a lot, thank you. But i have some problem when i want reset text on the timelabel,the text won't change,is any solution?
Hello,
when I call reset method to stop the timer, it resets the timer value back to initial set value and restarts counting down.
How can I stop the timer and set the timer back to 0:00:00?
I am using Xcode 7 and ios9.
thanks!
A great new feature, but I think it might take time to implement, would be to be able to make several timers synchro: by this I mean that the seconds change at the same moment for all the specified timers.
Hi, delegate should properly follow the pattern and be a weak reference. That way you prevent any circular reference.
In example 7 edit the line
[timerExample7 setCountDownTime:0];
When starting the example, by tapping "Start", alert does not appear. (Hitting "Start" once again, though, shows the alert).
When updateLabel
is called for the first time by a timer object, the endedBlock
is nil.
Change the order of sending start
and assigning the endedBlock
block.
-(void)startWithEndingBlock:(void(^)(NSTimeInterval))end{
self.endedBlock = end;
[self start];
}
For this case, though, the delegate is called.
Hi, great job!!
But, there is an issue, countdown time end block is not called properly, actually it is called 1 second before the finish time. Here is my code:
MZTimerLabel *countDownTimer = [[MZTimerLabel alloc] initWithLabel:secondsLabel andTimerType:MZTimerLabelTypeTimer];
[countDownTimer setCountDownTime:15];
countDownTimer.resetTimerAfterFinish = YES;
countDownTimer.timeFormat = @"mm:ss SS";
[countDownTimer startWithEndingBlock:^(NSTimeInterval countTime) {
// Called when 14 seconds passed
}];
Its not working in this scenario, please assist.
MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:someLabel andTimerType:MZTimerLabelTypeTimer];
[timer setShouldCountBeyondHHLimit:YES];
[timer setCountDownTime:3600 * 50];
[timer start];
It would be great if you allowed the timer to display more than 59 minutes if hours are not shown. Example 90:10. 90 minutes and 10 seconds.
Hi!
Thanks for the pod!
I'm currently having issues in a multiview application. I've a stopwatch running in a view A but if I change to view B and I return to A the stopwatch is stop.
How can I fix this behaviour ?
I create the timer and call...
self.countdown.setCountDownTime(60)
self.countdown.start()
but the timer label starts at 1 minute then starts to count up. Any help?
This project is great! Congrats! Maybe a simple and helpful features is a BOOL to check the timer is been started but in that moment it is in pause maybe like this:
@Property (assign,readonly) BOOL starting;
Hello,
First of all great little Library. I have got it all working however it always starts at 01:00.
The only thing I have done is set the time format to:
self.timerLabel.timeFormat = @"MM:ss";
Any clues?
Recent branches of CocoaPods support tvos as a platform:
CocoaPods/Core#263
Hi,
in my app I have 2 timer labels(countdown). I pause timers and resume few times. After 2 minutes it is delay first timer has about 1 second more. Where can be the problem?
Hello,
What is the best way to edit the timer.
For example we have a timer running and then a user can tap and edit the current time displayed. I edit the label and start but it starts from the previous time.
I have tried "setStopWatch" but it only accepts NSTimeInterval, is their a quick way to edit the timer?
Hello there! How being doing?
Thanks for this awesome pod! :)
I'm currently having issues while running a countdown at background. If somehow the counter is more then 20 minutes or so, it resets when i get back to the app... How can i avoid this?
Best!
Rafa
HI guys.
I encountered a problem when i use MZTimerLabel in collectioncell.
MZTimerLabel * test = [[MZTimerLabel alloc]initWithLabel:cell.timeLabel andTimerType:MZTimerLabelTypeTimer];
[test setCountDownTime:100];
[test start];
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.