我有一张图像,我想在白天显示为太阳,即从早上 6 点到下午 6 点,以及从下午 6 点到早上 6 点的月亮。
我已经成功实现了这一点,但问题是图像在达到指定时间时不会改变,除非在图像改变之前重新运行应用程序。
我不想使用 NSTimer 来检查时间,就像每秒一样。我想到的唯一可能的解决方案是使用 NSLocalNotification 但我是新手。有什么帮助吗?=)
-(void) dayOrNight
{
NSDate* date = [NSDate date];
NSDateFormatter* dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HHmm"];
NSString* dateString = [dateFormat stringFromDate:date];
NSNumber* currentTime = [NSNumber numberWithInt:[dateString intValue]];
NSNumber* daytime = [NSNumber numberWithInt:600];
NSNumber* nightime = [NSNumber numberWithInt:1800];
NSLog(@"current time: %@",dateString);
dayNight = [[UIImageView alloc] initWithFrame:CGRectMake(265, 10, 50, 50)];
if ( [currentTime doubleValue] >= [daytime doubleValue] && [currentTime doubleValue] <= [nightime doubleValue] )
{
dayNight.image = [UIImage imageNamed:@"Sun.png"];
}
else
{
dayNight.image = [UIImage imageNamed:@"moon.png"];
}
[self.view addSubview:dayNight];
}