简体   繁体   中英

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 .

-(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.

What you could consider in outline is:

  1. Your own timer class which provides a method, say tick , which causes the timer to progress. 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. Invalidating this NSTimer will stop the ticks, effectively pausing the custom timers. Creating an new NSTimer to provide the ticks will restart your custom timers.

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. You set it to bTime when you reset your system or create the bellProp object.

Use this value when you fire your timers instead of the bTime.

In your playSound method, you update this value with the current time interval.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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