`cancelPerformSelectorsWithTarget:` 与 `cancelPreviousPerformRequestsWithTarget:`
我正在构建一种“刻度”之间的间隔越来越小的模拟:
- (void) simulationTick {
if (self.currentTick >= kNumberOfSimulationTicks)
return; // recursion anchor
// ... do stuff ...
self.currentTick = self.currentTick + 1;
[self performSelector:@selector(simulationTick) withObject:nil
afterDelay:2.5 * pow(0.95,(double)self.currentTick)]; // acceleration
}
当离开模拟屏幕时,我想停止模拟,所以我用
[NSObject cancelPreviousPerformRequestsWithTarget:self]
我第一次尝试使用的方法
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self]
来做到这一点……但这不起作用。
为什么不呢?
I am building a kind of simulation with ever-smaller intervals between "ticks":
- (void) simulationTick {
if (self.currentTick >= kNumberOfSimulationTicks)
return; // recursion anchor
// ... do stuff ...
self.currentTick = self.currentTick + 1;
[self performSelector:@selector(simulationTick) withObject:nil
afterDelay:2.5 * pow(0.95,(double)self.currentTick)]; // acceleration
}
When leaving the Simulation screen, I want to stop the simulation, so I do this with
[NSObject cancelPreviousPerformRequestsWithTarget:self]
I first tried using
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self]
… but this didn’t work.
Why not?
Apparently there are many others who had the same problem and the same solution. But nowhere could I find exactly why. What’s the difference between the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论