在 iPhone 上实现闹钟的最有效方法
我已经实现了一个简单的时钟,如下所示:
- (void)runTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(showActivity)
userInfo:nil
repeats:YES];
}
- (void)showActivity {
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[df setDateFormat:@"HH:mm"];
[clock setFont:digitalFont];
[clock setText:[df stringFromDate:date]];
}
它基本上只是每秒运行一次以更新 UILabel 中的时间的任务。现在,如果我想扩展这个时钟以在某些时间做出反应,就像闹钟一样,该怎么办? 持续检查 NSDate 的值似乎很耗电。我该怎么做?
I have implemented a simple clock like so:
- (void)runTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(showActivity)
userInfo:nil
repeats:YES];
}
- (void)showActivity {
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[df setDateFormat:@"HH:mm"];
[clock setFont:digitalFont];
[clock setText:[df stringFromDate:date]];
}
It's basiclly just a task that is run every second to update the time in a UILabel. Now what if I would like to extend this clock to react on certain times, just like a alarm clock.
Continously checking the value of NSDate seems battery intensive. How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
initWithFireDate
方法You can use
initWithFireDate
method实现这一目标的最简单方法是什么?只需将另一个
NSTimer
设置为在警报发生时触发即可。The simplest way to achieve this? Just have another
NSTimer
that is set to fire at the time of the alarm.