根据时间变化的背景 - 目标C [英] Changing background according to time - Objective C

查看:110
本文介绍了根据时间变化的背景 - 目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想要做的是改变根据一天中的时间图像的背景,但它的运行了一个问题。这里的code:

So what I'm trying to do is change the background of an image according to the time of day, but it's running into a problem. Here's the code:

-(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!这不是选择,如果声明的权利!我该怎么办?

So what the problem is, let's say it's 11:54 PM, but it keeps saying that it's after 5:00 AM and before 11:59 AM! It's not choosing the right if statement! What do I do?

推荐答案

您应该使用 NSDateComponents 得到的只有小时和分钟的NSDate 你是对象比较。请参见官方文档 NSDateComponents 弄清楚如何使用它。

You should use the NSDateComponents to get only the hours and minutes of the NSDate objects that you are comparing. See the official documentation of NSDateComponents to figure out how to use it.

那么你应该比较的NSDate 使用下列方法之一对象:

Then you compare the NSDate objects using one of the following methods:

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

比较日期时,你不应该使用的NSString。

You should never use NSString when comparing dates.

这篇关于根据时间变化的背景 - 目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆