如何将 NSTimer 嵌套在另一个 NSTimer 中?

发布于 2024-09-11 08:47:56 字数 234 浏览 1 评论 0原文

我想要一个 NSTimer 每 1 秒执行一次,并有另一个 NSTimer 在该计时器内执行略小于 1 秒。

实际上我不确定最好的方法是什么,但我希望每秒出现一个随机图像,然后在那一秒后消失。

所以我需要某种停顿或可以打发时间的东西,然后再次执行隐藏按钮。

如果我将按钮隐藏设置为TRUE,然后将其设置为FALSE,那么出现时间会很短,我如何才能停止或进行第二遍然后再次隐藏图像?

提前致谢

I would like to have an NSTimer which executes every 1 second and have another NSTimer which executes within that timer slightly less than 1 second.

Actually I am not sure what is the best way to do this, but I would like a random image to appear every second and then disappear after after that second.

So I need some kind of stall or something which can pass the time and then execute the hide button again.

If I set the buttons hidden to TRUE and then set it to FALSE, this appearance time will be so short, how can I stall or make the second pass and then hide the image again?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦与时光遇 2024-09-18 08:47:56

您可以尝试将自己设置为在指定时间间隔后显示和隐藏对象的恒定循环,直到使用 performSelector:withObject:afterDelay: 方法在两个相反的方法中相互起作用:

//On initialization, make a first call to the hide method
//to be executed after a delay
-(void) viewDidload {
    [myObject setHidden:NO];
    [self performSelector:@selector(hideObject) withObject:nil afterDelay:1.0];
}

//Whenever the hide gets called, hide, then make a call to the show method
//to be executed after a delay
-(void) hideObject {
    [myObject setHidden:YES];
    [self performSelector:@selector(showObject) withObject:nil afterDelay:1.0];
}

//Whenever the show gets called, show, then make a call to the hide method
//to be executed after a delay
-(void) showObject {
    [myObject setHidden:NO];
    [self performSelector:@selector(showObject) withObject:nil afterDelay:1.0];
}

您可以通过放置一个 if 语句来中断循环,该语句为您检查 performSelector:withObject:afterDelay: 调用 。

You can try setting yourself into a constant cycle of showing and hiding the object after a specified interval until you interrupt it by using the performSelector:withObject:afterDelay: method reciprocally in two counter-acting methods:

//On initialization, make a first call to the hide method
//to be executed after a delay
-(void) viewDidload {
    [myObject setHidden:NO];
    [self performSelector:@selector(hideObject) withObject:nil afterDelay:1.0];
}

//Whenever the hide gets called, hide, then make a call to the show method
//to be executed after a delay
-(void) hideObject {
    [myObject setHidden:YES];
    [self performSelector:@selector(showObject) withObject:nil afterDelay:1.0];
}

//Whenever the show gets called, show, then make a call to the hide method
//to be executed after a delay
-(void) showObject {
    [myObject setHidden:NO];
    [self performSelector:@selector(showObject) withObject:nil afterDelay:1.0];
}

You can interrupt the cycle by putting an if statement which checks some end condition for you around the performSelector:withObject:afterDelay: call .

┼── 2024-09-18 08:47:56

我建议使用 NSTimer 的 initWithFireDate:interval:target:selector:userInfo:repeats:。这将使您可以定期触发计时器(如果将 repeats 设置为 YES),并且您可以指定 fireDate(第一个定时器被触发的时间)到立即为一个循环,稍晚为另一个循环。与 performSelector 相比,此方法的优点在于,可以通过向计时器发送 invalidate 轻松终止它,并且不会因您可能在其中任何一个中拥有的任何阻塞代码而延迟你的方法。

I would recommend using NSTimer's initWithFireDate:interval:target:selector:userInfo:repeats:. This will let you have the timer fire at a regular interval (if you set repeats to YES) and you can specify a fireDate (the first time the timer is fired) to immediately for one loop and slightly later for the other. What's better about this method compared to performSelector is that it can be easily terminated by sending invalidate to the timers and will not be delayed by any blocking code you might have in either of your methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文