NSTimer reverse count



  • Hello! Show me how to do it. There are 3 pickerView: number of repetitions, time of work, time of rest. We need a timer. Reverse count and displayed in the label. I can't start counting from the selected value and stop when 0 and start the report of the second chosen value and reduce the number of repeats by 1. In principle, with the number of repetitions, I understand what can be done in the cycle. But I can't figure it out.



  • Something like that.

    #import "ViewController.h"
    #import "MSWeakTimer.h"
    

    @interface ViewController () <UIPickerViewDataSource,UIPickerViewDelegate>

    @property (strong, nonatomic) IBOutletCollection(UIPickerView) NSArray *pickerView;

    @property (weak, nonatomic) IBOutlet UILabel *timeWorkText;
    @property (weak, nonatomic) IBOutlet UILabel *timeRestText;
    @property (weak, nonatomic) IBOutlet UILabel *roundText;

    @property (strong,nonatomic) NSArray * rounds;
    @property (strong,nonatomic) NSArray * timeWork;
    @property (strong,nonatomic) NSArray * timeRest;

    @property (strong, nonatomic) MSWeakTimer *timer;
    @property (strong, nonatomic) MSWeakTimer *backgroundTimer;

    @property (strong, nonatomic) dispatch_queue_t privateQueue;

    @property (nonatomic) int roundsCount;
    @property (nonatomic) int timeWorkCount;
    @property (nonatomic) int timeRestCount;
    @property (nonatomic) BOOL isWorking;
    @property (nonatomic) int countDown;

    @end

    @implementation ViewController

    static const char *MSSampleViewControllerTimerQueueContext = "MSSampleViewControllerTimerQueueContext";

    @synthesize
    pickerView = _pickerView;

    • (id)init
      {
      if ((self = [super init]))
      {
      self.privateQueue = dispatch_queue_create("com.mindsnacks.private_queue", DISPATCH_QUEUE_CONCURRENT);

        self.backgroundTimer = [MSWeakTimer scheduledTimerWithTimeInterval:1
                                                                    target:self
                                                                  selector:@selector(backgroundTimerDidFire)
                                                                  userInfo:nil
                                                                   repeats:YES
                                                             dispatchQueue:self.privateQueue];
      
        dispatch_queue_set_specific(self.privateQueue, (__bridge const void *)(self), (void *)MSSampleViewControllerTimerQueueContext, NULL);
      

      }

      return self;
      }

    • (void)dealloc
      {
      [_timer invalidate];
      [_backgroundTimer invalidate];
      }

    • (void)viewDidLoad {
      [super viewDidLoad];

      self.roundsCount = 1;

      //rounds
      NSMutableArray *mutableArrayRounds = [[NSMutableArray alloc] init];

      for (int rounds = 1; rounds < 16; rounds++) {

        [mutableArrayRounds addObject:[NSString stringWithFormat:@"%d",rounds]];
      

      }
      self.rounds = [NSArray arrayWithArray:mutableArrayRounds];

      //TimeRest

      NSMutableArray *mutableArrayTimeRest = [[NSMutableArray alloc] init];

      for (int min = 0; min < 6; min++)
      for (int sec = 0; sec < 60; sec++)
      {

            [mutableArrayTimeRest addObject:[NSString stringWithFormat:@"%d : %d",min, sec]];
        }
      

      self.timeRest = [NSArray arrayWithArray:mutableArrayTimeRest];

      //TimeWork

      NSMutableArray *mutableArrayTimeWork = [[NSMutableArray alloc] init];

      for (int min = 0; min < 6; min++)
      for (int sec = 0; sec < 60; sec++)
      {

            [mutableArrayTimeWork addObject:[NSString stringWithFormat:@"%d : %d", min, sec]];
        }
      

      self.timeWork = [NSArray arrayWithArray:mutableArrayTimeWork];

    }

    • (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
      }

    • (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
      return 1;

    }

    • (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

      NSInteger title = 0;

      if (pickerView.tag == 1) {
      title = [self.rounds count];
      }
      if (pickerView.tag == 2) {
      title =[self.timeRest count];
      }
      if (pickerView.tag == 3) {
      title =[self.timeWork count];

      }

      return title;
      }

    • (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

      NSString * titleRow;

      if (pickerView.tag == 1) {
      titleRow = [self.rounds objectAtIndex:row];
      }
      if (pickerView.tag == 2) {
      titleRow = [self.timeRest objectAtIndex:row];
      }
      if (pickerView.tag == 3) {
      titleRow = [self.timeWork objectAtIndex:row];
      }

      return titleRow;
      }

    • (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
      {
      if (pickerView.tag == 1) {
      self.roundsCount = [[self.rounds objectAtIndex:row] integerValue];
      }
      if (pickerView.tag == 2) {
      self.timeWorkCount = [self convertTime:[self.timeRest objectAtIndex:row]];
      }
      if (pickerView.tag == 3) {
      self.timeRestCount = [self convertTime:[self.timeWork objectAtIndex:row] ];
      }

    }

    #pragma mark actions

    • (IBAction)cancelButtonPressed:(id)sender {
      self.roundText.text = 0;
      self.timeWorkText.text = 0;
      self.timeRestText.text = 0;

    }

    • (IBAction)goButtonPressed:(id)sender {

      {
      static NSString *kStopTimerText = @"Stop";
      static NSString *kStartTimerText = @"Start";

        NSString *currentTitle = [sender titleForState:UIControlStateNormal];
      
        if ([currentTitle isEqualToString:kStopTimerText])
        {
            [sender setTitle:kStartTimerText forState:UIControlStateNormal];
            [self.timer invalidate];
        }
        else
        {
            self.roundText.text = [NSString stringWithFormat:@"%i", self.roundsCount];
            self.isWorking = YES;
            self.countDown = self.timeWorkCount;
      
            [sender setTitle:kStopTimerText forState:UIControlStateNormal];
            self.timer = [MSWeakTimer scheduledTimerWithTimeInterval:1
                                                              target:self
                                                            selector:@selector(mainThreadTimerDidFire:)
                                                            userInfo:nil
                                                             repeats:YES
                                                       dispatchQueue:dispatch_get_main_queue()];
        }
      

      }

    }

    • (int)convertTime:(NSString*)time
      {
      NSLog(@"time %@", time);
      long minutes = [[time substringToIndex:1] integerValue];
      long seconds = [[time substringFromIndex:3] integerValue];
      return minutes*60+seconds;
      }

    #pragma mark - mainThreadTimer

    • (void)mainThreadTimerDidFire:(MSWeakTimer *)timer
      {
      NSAssert([NSThread isMainThread], @"This should be called from the main thread");

      self.countDown--;

      if(self.isWorking) {
      self.timeWorkText.text = [NSString stringWithFormat:@"%i", self.countDown];
      } else {
      self.timeRestText.text = [NSString stringWithFormat:@"%i", self.countDown];
      }

      if(self.countDown <= 0) {
      if(self.isWorking) {
      self.isWorking = NO;
      self.countDown = self.timeRestCount;
      } else {
      self.roundsCount--;
      self.roundText.text = [NSString stringWithFormat:@"%i", self.roundsCount];
      if(self.roundsCount <= 0) {
      [self.timer invalidate];
      return;
      }
      self.isWorking = YES;
      self.countDown = self.timeWorkCount;
      }
      }
      }

    #pragma mark backgroundTimer

    • (void)backgroundTimerDidFire
      {
      NSAssert(![NSThread isMainThread], @"This shouldn't be called from the main thread");

      const BOOL calledInPrivateQueue = dispatch_queue_get_specific(self.privateQueue, (__bridge const void *)(self)) == MSSampleViewControllerTimerQueueContext;
      NSAssert(calledInPrivateQueue, @"This should be called on the provided queue");
      }

    @end




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2