iOS如何每分钟在每分钟调用一次方法 [英] iOS how to call a method on the minute, every minute

查看:82
本文介绍了iOS如何每分钟在每分钟调用一次方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是12:01:00、12:02:00 ...

I mean 12:01:00, 12:02:00 ...

在iOS7中,我想在更改分钟的时间时调用一个方法.换句话说,如果现在是01:55:47,那么我想在01:56:00-> 01:57:00-> 01:58:00调用方法...

In iOS7, I want to call a method when minute of time is changed. In other words, if now is 01:55:47, then I want to call a method at 01:56:00 -> 01:57:00 -> 01:58:00 ...

我发现有关调用方法的全部只是有关TimeInterval的信息,而不是我想要的.

All I found about call a method is just about TimeInterval, but not I want.

我尝试了以下代码:

NSTimeInterval roundedInterval = round([[NSDate date] timeIntervalSinceReferenceDate] / 60.0) * 60.0;
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:roundedInterval];

NSTimer *timer = [[NSTimer alloc] initWithFireDate:date
                                          interval:60.0
                                            target:self
                                          selector:@selector(handleEveryMinutes:)
                                          userInfo:nil
                                           repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

- (void)handleEveryMinutes:(NSTimer *)timer {
NSLog(@"one minute!");
}

但是此方法不会在第二个0处调用.

But this method is not called at second 0.

希望很酷的人可以帮助我!

Hope cool people can help me!

-----我的答案-------

----- My answer -------

我还弄清楚了为什么我的代码无法正常工作,我需要在roundedInterval上增加额外的60秒,这是下一分钟的确切时间.如果不加60秒,将通过fireDate,因此当我运行我的应用程序时,必须立即启动.

I also figure out why my code is not working correctly, I need add extra 60 seconds to the roundedInterval, which is the exact time of the next minute. If do not add 60 second, the fireDate is passed, so when I run my app, it have to fire immediately.

NSTimeInterval roundedInterval = round([[NSDate date] timeIntervalSinceReferenceDate] / 60.0) * 60.0 + 60; // the extra 60 is used to set the correct time Interval between next minute and referenced date.
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:roundedInterval];

现在,它正在工作!

推荐答案

关键是在正确的时间启动该循环计时器.获取当前时间并找出距当前分钟有几秒钟...

The key is to start that recurring timer at the right time. Get the current time and find out how many seconds we are into the current minute...

NSDateComponents *components = [[NSCalendar currentCalendar] components: NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger second = [components second];

由此,我们可以获取到下一分钟的秒数...

From this we can get the number of seconds until the next minute...

NSInteger tillNextMinute = (60 - second) % 60;

我还没有测试过,但是mod 60的想法是处理秒为零的情况.现在我们快完成了...

I haven't tested that, but the idea of the mod 60 is to handle the case when second is zero. Now we're almost done...

[self performSelector:@selector(startTimer) withObject:nil afterDelay:tillNextMinute];

然后您开始的代码...

Then the code you began with...

- (void)startTimer {
    // contains the code you posted to start the timer
}

这篇关于iOS如何每分钟在每分钟调用一次方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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