根据时间切换背景

时间:2014-11-04 14:51:01

标签: objective-c ios8 xcode6

所以我查看了堆栈溢出,了解如何根据当前时间使用IF语句切换背景图像& NSDateComponents:

NSDateComponents *components = [NSDateComponents new];
//AM
NSInteger Hour06 = 6; //1EarlyMorning
NSInteger Hour08 = 8; //2Morning
NSInteger Hour11 = 11; //3LateMorning
//PM
NSInteger Hour12 = 12; //4Afternoon
NSInteger Hour16 = 16; //5LateAfternoon
NSInteger Hour18 = 18; //6Evening
NSInteger Hour20 = 20; //7LateEvening
NSInteger Hour21 = 21; //8Night
NSInteger Hour23 = 23; //9LateNight


if (components.hour < Hour18)
{
    self.backgroundImage = [SKSpriteNode spriteNodeWithImageNamed:@"6Evening"];
    self.backgroundImage.position = CGPointMake(self.size.width/2, self.size.height/2);
    self.backgroundImage.zPosition = 0.0; //Default bottom position

    [self addChild:self.backgroundImage];
}

但图像不会改变。我已经读过Changing background according to time - Objective C&amp;它也没有帮助。有人能告诉我我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我的自我&amp; YvesLeBorg发现了答案!如果您因使用Sprite Kit而使用SKView,则以下情况有效:

-(void)background
{

    NSDateComponents *components = [NSDateComponents new];
    NSInteger Hour18 = 18; //6Evening


    if (components.hour > Hour18)
    {
        self.backgroundImage = [SKSpriteNode spriteNodeWithImageNamed:@"6Evening"];
        self.backgroundImage.position = CGPointMake(self.size.width/2, self.size.height/2);
        self.backgroundImage.zPosition = 0.0; //Default bottom position

        [self addChild:self.backgroundImage];
    }
}