gpt4 book ai didi

ios - 上午 6 点更改背景图像一次,下午 6 点更改背景图像

转载 作者:行者123 更新时间:2023-11-29 00:41:08 25 4
gpt4 key购买 nike

我有两张图片,一张用于早上,我需要在早上 6 点之后显示,另一张用于晚上,我需要在下午 6 点之后在背景中显示。请帮助我在 6am-6pm 和 6pm-6am 自动更改显示。

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"];
}

最佳答案

尝试像下面这样使用NSDateComponents

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[NSDate date]];
NSInteger nowHour = [components hour];
NSInteger nowMinute = [components minute];
NSInteger nowSecond = [components second];

if (nowHour < 6 || (nowHour > 18 || (nowHour == 18 && (nowMinute > 0 || nowSecond > 0)))) {
// Time is 6pm to 6am
}
else{
// Time is 6am to 6pm

关于ios - 上午 6 点更改背景图像一次,下午 6 点更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com