[英]iPhone sdk: Countdown timer
Please help. 请帮忙。 I want to create countdown timer using iphone sdk.
我想用iphone sdk创建倒数计时器。 I have current time:
我有现在的时间:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
and time(s) from database. 和来自数据库的时间。 Eg.: currentTime is 19:18:30, time from database is 19:20:01.
例如:currentTime是19:18:30,数据库的时间是19:20:01。 So the timer should be set to 0:01:31 and begin the countdown.
因此,计时器应设置为0:01:31并开始倒计时。 When reach the 0:00:00, take another time from database (eg: 19:21:00), set up timer (0:00:59) and star countdown again.
当到达0:00:00时,从数据库中取出另一个时间(例如:19:21:00),再次设置计时器(0:00:59)和星号倒计时。 When reach the 0:00:00, take... The timer should be displayed by UILabel.
当到达0:00:00时,采取...计时器应由UILabel显示。
Please help. 请帮忙。 It begins to make me mad.
它开始让我发疯。 :)
:)
try this. 尝试这个。 I've just written only required methods for the solution.
我刚刚只编写了解决方案所需的方法。 Don't forget to implement dealloc etc.
不要忘记实现dealloc等。
CountdownViewController.h CountdownViewController.h
@interface CountdownViewController : UIViewController {
IBOutlet UILabel *clockLabel;
NSDate *databaseDate;
NSTimer *timer;
}
@property(nonatomic, retain) IBOutlet UILabel *clockLabel;
@property(nonatomic, retain) NSDate *databaseDate;
@end
CountdownViewController.m CountdownViewController.m
@implementation CountdownViewController
@synthesize clockLabel;
@synthesize databaseDate;
- (void) showClock {
NSTimeInterval remainingSec = [databaseDate timeIntervalSinceNow];
if (!timer || remainingSec <= 0) {
[timer invalidate];
timer = nil;
// getting time from database
self.databaseDate = [NSDate dateWithTimeIntervalSinceNow:20.0];
remainingSec = [databaseDate timeIntervalSinceNow];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(showClock)
userInfo:nil
repeats:YES];
}
NSInteger hours = remainingSec / 3600;
NSInteger remainder = ((NSInteger)remainingSec)% 3600;
NSInteger minutes = remainder / 60;
NSInteger seconds = remainder % 60;
clockLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.