Swift2:在iOS中重复检查时间间隔
因为我有三个时间间隔,一个小于15分钟,第二个大于或等于15,第三个大于或等于15 30.在我的情况下,我只检查一次条件。但我想每1分钟检查一次条件。因为在特定的时间间隔后,我所有的按钮颜色都应该仅更改为appYellowColor()。任何帮助都是值得的。提前感谢!这是我的密码Swift2:在iOS中重复检查时间间隔,ios,swift,uibutton,uicolor,nstimeinterval,Ios,Swift,Uibutton,Uicolor,Nstimeinterval,因为我有三个时间间隔,一个小于15分钟,第二个大于或等于15,第三个大于或等于15 30.在我的情况下,我只检查一次条件。但我想每1分钟检查一次条件。因为在特定的时间间隔后,我所有的按钮颜色都应该仅更改为appYellowColor()。任何帮助都是值得的。提前感谢!这是我的密码 if NetworkService.weAreOnline(true) && interval/60 < 15 { print("appYellowColor()")
if NetworkService.weAreOnline(true) && interval/60 < 15 {
print("appYellowColor()")
startButton.backgroundColor = UIColor.appYellowColor()
}
else if NetworkService.weAreOnline(true) && interval/60 >= 15 && interval/60 < 30 {
print("grayColor()")
startButton.backgroundColor = UIColor.grayColor()
startButton.userInteractionEnabled = false
}
else if NetworkService.weAreOnline(true) && interval/60 >= 30 {
print("redColor()")
startButton.backgroundColor = UIColor.redColor()
startButton.userInteractionEnabled = false
}
如果NetworkService.weAreOnline(true)和&interval/60<15{
打印(“appYellowColor()”)
startButton.backgroundColor=UIColor.appYellowColor()
}
否则,如果NetworkService.weAreOnline(true)和&interval/60>=15和&interval/60<30{
打印(“灰度()”)
startButton.backgroundColor=UIColor.grayColor()
startButton.userInteractionEnabled=false
}
如果NetworkService.weAreOnline(true)和&interval/60>=30,则为else{
打印(“redColor()”)
startButton.backgroundColor=UIColor.redColor()
startButton.userInteractionEnabled=false
}
支持重复任务的一种可能方法是将其逻辑放入函数中(在您的情况下可以是checkIsOnline()
),并使用repeats选项安排计时器
let timer = Timer(fire: Date.init(timeIntervalSinceNow: 60), interval: 60, repeats: true) { (timer) in
self.checkIsOnline()
}
RunLoop.main.add(timer, forMode: .defaultRunLoopMode)
当应用程序进入后台时,您可以使计时器失效()
,当应用程序即将激活时,您可以重新创建计时器。支持重复任务的一种可能方法是将其逻辑放入函数中(在您的情况下,它可以是checkIsOnline()
),并使用repeats选项安排一个计时器
let timer = Timer(fire: Date.init(timeIntervalSinceNow: 60), interval: 60, repeats: true) { (timer) in
self.checkIsOnline()
}
RunLoop.main.add(timer, forMode: .defaultRunLoopMode)
当应用程序进入后台时,您可以使计时器失效()
,并在应用程序即将激活时重新创建计时器。无需代码,只需理论
您需要第四个计时器。一组为一分钟(60秒)。如果与其他三个同时启动(或紧接着启动),则只需检查其他三个设置的标志。(如果这三个计时器中的任何一个或全部都重复了,那么在管道中发送消息时,请让第四个计时器复位。无需代码,只需理论即可
您需要一个第四个计时器。一个计时器设置一分钟(60秒)。如果与其他三个计时器同时启动(或紧接着启动),则只需检查其他三个计时器设置的标志。(如果这三种方法中的任何一种或全部被重复,在管道中发送消息时,让第四个计时器重置事情。我不知道swift,但在目标c中是这样的。如果你以特定的时间间隔连续调用特定的方法,你可以使用NSTimer 第一件事是你的第二个声音时间间隔大于或等于15。间隔/60>=15如果我们放置这个,它不会转到第三个,因为它总是正确的,所以我们需要保持这样 NetworkService.weAreOnline(true)&&interval/60>=15&&interval/60<30当它失败时,将进入第三种状态
NSTimer *reachTimer;
//NSInteger secondsLeft;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//to stop the timer you can use secondsLeft
//secondsLeft = 0;
reachTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
}
// This method is called every 300 secs
-(void)updateCounter:(NSTimer *)theTimer{
// Build your login here
// //if you want to stop timer after 300 sec you can use below code
// secondsLeft = secondsLeft+5;
// if(secondsLeft>=300) // 60sec * 5min= 300
// {
// [reachTimer invalidate];// stop timer
// }
}
我不知道swift,但在目标c中是这样的。如果你以特定的时间间隔连续调用特定的方法,你可以使用NSTimer 第一件事是你的第二个声音时间间隔大于或等于15。间隔/60>=15如果我们放置这个,它不会转到第三个,因为它总是正确的,所以我们需要保持这样 NetworkService.weAreOnline(true)&&interval/60>=15&&interval/60<30当它失败时,将进入第三种状态
NSTimer *reachTimer;
//NSInteger secondsLeft;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//to stop the timer you can use secondsLeft
//secondsLeft = 0;
reachTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
}
// This method is called every 300 secs
-(void)updateCounter:(NSTimer *)theTimer{
// Build your login here
// //if you want to stop timer after 300 sec you can use below code
// secondsLeft = secondsLeft+5;
// if(secondsLeft>=300) // 60sec * 5min= 300
// {
// [reachTimer invalidate];// stop timer
// }
}
在这种情况下轮询是非常糟糕的。考虑使用NealService,当条件改变时通知它。不要问,TeleSwitter 2已经死了。更新。@ ViaTyNVR NPOLLIN在这种情况下是非常糟糕的。考虑使用网络服务,当条件改变时通知它。不要问,TelSwitter 2已经死了。