根据目标C的改变背景



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

-(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之前!它不是选择正确的语句!我该怎么办?

可能会帮助您,

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

nsdate格式提供24小时格式的时间,因此您必须更改时间(01:00 am,13:00 pm),以便将条件放置在您想要的任何情况下。

您应该使用 nsdatecomponents 仅获取您要比较的 nsdate 对象的小时和分钟。请参阅 nsdatecomponents的官方文档弄清楚如何使用它。

然后,使用以下方法之一比较 nsdate 对象:

- (NSDate *)earlierDate:(NSDate *)anotherDate
- (NSDate *)laterDate:(NSDate *)anotherDate
- (BOOL)isEqualToDate:(NSDate *)anotherDate

比较日期时,切勿使用NSString。

相关内容

  • 没有找到相关文章

最新更新