1

我有一张图像,我想在白天显示为太阳,即从早上 6 点到下午 6 点,以及从下午 6 点到早上 6 点的月亮。

我已经成功实现了这一点,但问题是图像在达到指定时间时不会改变,除非在图像改变之前重新运行应用程序。

我不想使用 NSTimer 来检查时间,就像每秒一样。我想到的唯一可能的解决方案是使用 NSLocalNotification 但我是新手。有什么帮助吗?=)

-(void) dayOrNight
{

NSDate* date                = [NSDate date];

NSDateFormatter* dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HHmm"];
NSString* dateString        = [dateFormat stringFromDate:date];
NSNumber* currentTime       = [NSNumber numberWithInt:[dateString intValue]];
NSNumber* daytime           = [NSNumber numberWithInt:600];
NSNumber* nightime          = [NSNumber numberWithInt:1800];
NSLog(@"current time: %@",dateString);

dayNight = [[UIImageView alloc] initWithFrame:CGRectMake(265, 10, 50, 50)];


    if ( [currentTime doubleValue] >= [daytime doubleValue] && [currentTime doubleValue] <= [nightime doubleValue] ) 
    {
    dayNight.image = [UIImage imageNamed:@"Sun.png"];
    }

    else 
    {
    dayNight.image = [UIImage imageNamed:@"moon.png"];
    }


[self.view addSubview:dayNight];
}
4

3 回答 3

0

您无需每秒钟检查时间。

这是可能的解决方案之一。它仅在真实设备上进行了测试


你的UIYourViewController.m

- (void)viewWillAppear:(BOOL)animated {
    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
        dayNight.image = [UIImage imageNamed:@"moon.png"];
        [self performSelector:@selector(replaceTheBackgoundForMorning) withObject:nil afterDelay:_intervalToMorningDate];
    } else {
        // now it is daytime
        dayNight.image = [UIImage imageNamed:@"Sun.png"];
        [self performSelector:@selector(replaceTheBackgoundForEvening) withObject:nil afterDelay:_intervalToEveningDate];
    }

}

- (void)viewDidDisappear:(BOOL)animated {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

最后你应该将它们添加到同一个.m文件中:

-(void)replaceTheBackgoundForMorning {
    // reaplce the backgound here
    dayNight.image = [UIImage imageNamed:@"Sun.png"];
}

- (void)replaceTheBackgoundForEvening {
    // reaplce the backgoung here
    dayNight.image = [UIImage imageNamed:@"moon.png"];
}
于 2012-08-17T09:15:33.223 回答
0

我认为没有 NSTimer 是不可能的。您可以自己设置 refreshTime(例如 1 分钟/如果您不想每秒都这样做)))或者您可以在其他方法中调用此方法,这些方法每次都在您的班级中工作......

也许你有一些东西,你在课堂上使用的东西正在工作......

-(IBAction)myButtonWhichIPressDuringIworkHere {
///some actions
[self dayOrNight];
}

在其他情况下,您应该使用 NSTimer

于 2012-08-17T08:59:37.883 回答
0

我猜本地通知应该没问题。 在这里,您可以获得所有需要的代码片段以在需要的时间执行dayOrNight方法。此外,您不应该在每次更改图片时添加新视图。

于 2012-08-17T09:03:09.447 回答