gpt4 book ai didi

time - 根据时间改变背景 - Objective C

转载 作者:行者123 更新时间:2023-12-02 05:04:56 24 4
gpt4 key购买 nike

所以我想做的是根据一天中的时间更改图像的背景,但它遇到了问题。这是代码:

-(void)updateBackground
{
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *resultTime = [dateFormatter stringFromDate:currentTime];
[timeLabel setText:resultTime];
NSString *morningStart = [NSString stringWithFormat:@"05:00 AM"];
NSString *morningEnd = [NSString stringWithFormat:@"11:59 AM"];
NSString *afternoonStart = [NSString stringWithFormat:@"12:00 PM"];
NSString *afternoonEnd = [NSString stringWithFormat:@"02:59 PM"];
NSString *eveningStart = [NSString stringWithFormat:@"03:00 PM"];
NSString *eveningEnd = [NSString stringWithFormat:@"07:59 PM"];
NSString *nightStart = [NSString stringWithFormat:@"08:00 PM"];
NSString *nightEnd = [NSString stringWithFormat:@"04:59 AM"];
NSLog(resultTime);
if ([resultTime compare:morningStart]==NSOrderedDescending) {
if ([resultTime compare:morningEnd]==NSOrderedAscending) {
UIImage *morning = [UIImage imageNamed:@"MorningTime.jpeg"];
[background setImage:morning];
}
}
if ([resultTime compare:afternoonStart]==NSOrderedDescending) {
if ([resultTime compare:afternoonEnd]==NSOrderedAscending) {
UIImage *afternoon = [UIImage imageNamed:@"afternoonSky.jpeg"];
[background setImage:afternoon];
}
}
if ([resultTime compare:eveningStart]==NSOrderedDescending) {
if ([resultTime compare:eveningEnd]==NSOrderedAscending) {
UIImage *evening = [UIImage imageNamed:@"EveningSky.jpeg"];
[background setImage:evening];
}
}
if ([resultTime compare:nightStart]==NSOrderedDescending) {
if ([resultTime compare:nightEnd]==NSOrderedAscending) {
UIImage *night = [UIImage imageNamed:@"NightSky.jpeg"];
[background setImage:night];
}
}


}

那么问题出在哪里,假设现在是晚上 11:54,但它一直说是早上 5:00 之后和上午 11:59 之前!不是选择正确的 if 语句!我该怎么办?

最佳答案

也许这个对你有帮助,

NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *resultTime = [dateFormatter stringFromDate:currentTime];
NSString *morningStart = [NSString stringWithFormat:@"05:00 AM"];
NSString *morningEnd = [NSString stringWithFormat:@"11:59 AM"];
NSString *afternoonStart = [NSString stringWithFormat:@"12:00 PM"];
NSString *afternoonEnd = [NSString stringWithFormat:@"14:59 PM"];
NSString *eveningStart = [NSString stringWithFormat:@"15:00 PM"];
NSString *eveningEnd = [NSString stringWithFormat:@"19:59 PM"];
if ([morningStart compare:resultTime]==NSOrderedAscending & [resultTime compare:morningEnd]==NSOrderedAscending) {
NSLog(@"morning");
}
else if ([afternoonStart compare:resultTime]==NSOrderedAscending & [resultTime compare:afternoonEnd]==NSOrderedAscending) {
NSLog(@"aftrnon...");
}
else if ([eveningStart compare:resultTime]==NSOrderedAscending & [resultTime compare:eveningEnd]==NSOrderedAscending) {
NSLog(@"evening");
}
else {
NSLog(@"nit time");
}

关于time - 根据时间改变背景 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12995103/

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