Bau*_*aub 9 iphone objective-c nstimer ios
我有一个应用程序,应该每1秒记录一些事情,我正在使用NSTimer
,但如果我的应用程序转换屏幕(或几乎任何其他,真的)它会减慢计时器一点点使不准确的读数.
什么是可靠的替代品使用?我目前的代码如下:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
NJo*_*nes 13
NSTimer无法保证准时按时发射.但是你可以以比现在更可靠的方式使用NSTimer.当您使用时scheduledTimerWithTimeInterval
,创建一个在运行循环中安排的NSTimer NSDefaultRunLoopMode
.在使用UI时,此模式暂停,因此在有用户交互时,您的计时器不会触发.要避免此暂停,请使用该模式NSRunLoopCommonModes
.要做到这一点,你必须自己安排计时器,如下所示:
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(update) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3956 次 |
最近记录: |