昼間、つまり午前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];
}