【问题标题】:iPhone Digital ClockiPhone 数字时钟
【发布时间】:2011-02-03 02:00:25
【问题描述】:

在我花了很长时间让模拟时钟工作之后,我正在尝试制作一个数字时钟(sigh)。我正在尝试使用 10 个数字为 0 - 9 的 PNG 来执行此操作。时钟的每个数字都是一个图像。唯一的问题是从当前时间检索某个数字。我尝试将时间转换为字符串并使用 characterAtIndex,但这似乎不起作用。解决这个问题的最佳方法是什么?

【问题讨论】:

  • 嗨@Andy 解决了你的问题,因为我和你的问题一样。请你帮我制作一个包含 10 张图像 (0-9) 的数字。

标签: ios timer nsstring clock digital


【解决方案1】:

您可以使用NSDateComponentsNSCalendar

NSDate * date = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * components =
                    [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];

NSInteger hour = [weekdayComponents hour];
NSInteger minute = [weekdayComponents minute];
NSInteger firstHourDigit = hour/10;
NSInteger secondHourDigit = hour%10;
NSInteger firstMinuteDigit = minute/10;
NSInteger secondMinuteDigit = minute%10;

【讨论】:

  • 感谢您的回复,这是一个有趣的方法。我没想到会这么容易!我很快就会试试看。
  • 这不起作用...我尝试使用这个:[minOne setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", secondMinuteDigit]]];我做错了什么?
  • 即使我使用的是 nstimer,它也不会更新图像...但是图像确实显示出来了。
猜你喜欢
  • 2011-06-15
  • 2010-10-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2012-07-09
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多