Last active
May 27, 2024 12:09
-
-
Save dokun1/9394243 to your computer and use it in GitHub Desktop.
UILabel as countdown timer given NSTimeInterval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void) updateCountdown { | |
NSTimeInterval timeToClosing = [_deleteDate timeIntervalSinceDate:[NSDate date]]; | |
div_t h = div(timeToClosing, 3600); | |
int hours = h.quot; | |
div_t m = div(h.rem, 60); | |
int minutes = m.quot; | |
int seconds = m.rem; | |
NSString *hoursStr, *minutesStr, *secondsStr; | |
if (hours < 10) | |
hoursStr = [NSString stringWithFormat:@"0%d", hours]; | |
else | |
hoursStr = [NSString stringWithFormat:@"%d", hours]; | |
if (minutes < 10) | |
minutesStr = [NSString stringWithFormat:@"0%d", minutes]; | |
else | |
minutesStr = [NSString stringWithFormat:@"%d", minutes]; | |
if (seconds < 10) | |
secondsStr = [NSString stringWithFormat:@"0%d", seconds]; | |
else | |
secondsStr = [NSString stringWithFormat:@"%d", seconds]; | |
if (seconds < 0) | |
[self dismissButtonTapped]; | |
else | |
self.countdownLabel.text = [NSString stringWithFormat:@"%@:%@:%@", hoursStr, minutesStr, secondsStr]; | |
if (timeToClosing <= 30) { | |
[_switchTimer invalidate]; | |
_switchTimer = nil; | |
[UIView animateWithDuration:0.7 | |
animations:^{ | |
self.eventEndsLabel.alpha = 0.0f; | |
self.countdownLabel.alpha = 1.0f; | |
}]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment