iPhone Digital Clock

亡梦爱人 提交于 2020-01-16 13:27:28

问题


After the long time I spent getting an analog clock to work, I am trying to make a digital one (sigh). I am trying to do this with 10 PNGs with the numbers 0 - 9. Each digit of the clock would be an image. The only problem with this is retrieving the certain digit from the current time. I've tried converting the time to a string and using the characterAtIndex, but that doesn't seem to work. What would be the best way to get around this?


回答1:


You could use NSDateComponents and NSCalendar:

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;


来源:https://stackoverflow.com/questions/4881992/iphone-digital-clock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!