午前6時以降に表示する必要がある2枚の画像と、午後6時以降に背景に表示する必要がある夜用画像があります。午前6時〜午後6時と午後6時〜午前6時に自動的に表示を変更してください。背景画像を午前6時に1回、午後6時に変更する
NSDate *_currentDate = [NSDate date];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:@"yyyy-MM-dd' 06:00AM +0000'"]; // set the morning date here
NSString *_morningDateString = [_dateFormatter stringFromDate:_currentDate];
[_dateFormatter setDateFormat:@"yyyy-MM-dd' 06:00PM +0000'"]; // set the evening date here
NSString *_eveningDateString = [_dateFormatter stringFromDate:_currentDate];
[_dateFormatter setDateFormat:@"yyyy-MM-dd hh:mma zzz"];
NSDate *_morningDate = [_dateFormatter dateFromString:_morningDateString];
NSDate *_eveningDate = [_dateFormatter dateFromString:_eveningDateString];
NSTimeInterval _intervalToMorningDate = [_morningDate timeIntervalSinceDate:_currentDate];
NSTimeInterval _intervalToEveningDate = [_eveningDate timeIntervalSinceDate:_currentDate];
if (_intervalToMorningDate > 0) {
// now it is night
self.bgImage.image = [UIImage imageNamed:@"main_bg.png"];
[self performSelector:@selector(replaceTheBackgoundForMorning) withObject:nil afterDelay:_intervalToMorningDate];
} else {
// now it is daytime
self.bgImage.image = [UIImage imageNamed:@"daylight.png"];
[self performSelector:@selector(replaceTheBackgoundForEvening) withObject:nil afterDelay:_intervalToEveningDate];
}
-(void)replaceTheBackgoundForMorning {
// reaplce the backgound here
self.bgImage.image = [UIImage imageNamed:@"daylight.png"];
}
- (void)replaceTheBackgoundForEvening {
// reaplce the backgoung here
self.bgImage.image = [UIImage imageNamed:@"main_bg.png"];
}
これはあまりにも複雑に思えます。ちょうど1分ごとに刻々と変化するNSTimerを作成し、タイマーハンドラで現在の時間を見て、画像を適切に設定してください。 – Paulw11