简体   繁体   English

如何暂停和恢复多个NSTimer?

[英]How to Pause and Resume Multiple NSTimer?

I want to play a sound (bells) with multiple scheduledTimerWithTimeInterval set like Every 5, Every 9 Every 35 after 6 ....etc now I Bells start playing with Interval 55, 56, 59, 60, 45, 42....but i want to pause time on 52 then i invalidate all timer because not got pause method or property of NSTimer after that I again start timer is start with new interval like 48 , 47 ,43 .... but I want maintain old Interval So any one have idea about it please help me . 我想播放具有多个ScheduledTimerWithTimeInterval的声音(铃声),例如每5,每9,每6之后的35 .... etc。现在,我的铃声开始以55、56、59、60、45、42等间隔播放。但是我想在52上暂停时间,然后我使所有计时器无效,因为没有暂停方法或NSTimer的属性之后,我再次启动计时器是从新的间隔开始,如48,47,43 ....但我想保持旧的间隔,任何人对此都有想法请帮助我。

-(void) bellsSchedual {
    arrBellsListAllData = [DBModel getDataFromBellsList:prop.userId];
    DBProperty *bellProp = [[DBProperty alloc]init];

    for (int i = 0; i < arrBellsListAllData.count; i++) {
        bellProp=[arrBellsListAllData objectAtIndex:i]; 
        NSString* bellsTime=bellProp.bTime; 
        if ([bellProp.bTimeSchedule isEqualToString: @"after"]) {
            NSTimer* bellTimer = [NSTimer scheduledTimerWithTimeInterval: [bellsTime intValue] 
                                                                  target: self 
                                                                selector: @selector(playSound:) 
                                                                userInfo: nil 
                                                                 repeats: NO]
            [arrTimers addObject:bellTimer]; 
        } else if ([bellProp.bTimeSchedule isEqualToString: @"every"]) {
            NSTimer* bellTimer = [NSTimer scheduledTimerWithTimeInterval: [bellsTime intValue] 
                                                                  target: self 
                                                                selector: @selector(playSound:) 
                                                                userInfo: nil 
                                                                 repeats: YES]; 
            [arrTimers addObject: bellTimer]; 
        } 
   } 
} 

Thanks 谢谢

If I understand your question correctly you require a timer that can be paused, and have correctly determined that an NSTimer cannot be paused. 如果我正确理解了您的问题,则需要一个可以暂停的计时器,并正确确定了NSTimer无法暂停。

What you could consider in outline is: 您可以在大纲中考虑的是:

  1. Your own timer class which provides a method, say tick , which causes the timer to progress. 您自己的计时器类提供了一个方法,例如tick ,它使计时器前进。 Use an instance of this class for each bell; 为每个铃铛使用此类的实例; and
  2. Use a repeating NSTimer to provide the tick - when it fires it calls the tick methods of all your registered custom timers. 使用重复的NSTimer来提供刻度线-触发时,它将调用所有已注册的自定义计时器的tick方法。 Invalidating this NSTimer will stop the ticks, effectively pausing the custom timers. 使此NSTimer无效会使刻度线停止,从而有效地暂停自定义计时器。 Creating an new NSTimer to provide the ticks will restart your custom timers. 创建一个新的NSTimer来提供刻度,将重新启动您的自定义计时器。

HTH HTH

Question is not really clear… As far as I understand, each bell has it's own timer and varying interval time. 问题还不是很清楚……据我了解,每个钟声都有自己的计时器,并且间隔时间各不相同。

Add a property bCurrentTime to your bellProp. 将属性bCurrentTime添加到bellProp。 You set it to bTime when you reset your system or create the bellProp object. 重置系统或创建bellProp对象时,可以将其设置为bTime。

Use this value when you fire your timers instead of the bTime. 当您启动计时器而不是bTime时,请使用此值。

In your playSound method, you update this value with the current time interval. 在playSound方法中,使用当前时间间隔更新此值。

This way, when you invalidate all your timers and restart them, all previous times are stored. 这样,当您使所有计时器失效并重新启动它们时,将存储所有以前的时间。

Add a reset method to set all bells bCurrentTime to bTime. 添加重置方法以将所有铃声bCurrentTime设置为bTime。

If you want control over your timers, you need to make them properties. 如果要控制计时器,则需要为其设置属性。 That way you can start, stop, and do whatever you want within the entire scope of the file under which you declared them. 这样,您可以在声明它们的文件的整个范围内启动,停止和执行所需的任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM