Calculate current time from next minimum, then NSTimer should not work

Hi, I am new to iOS. Now I have confused this concept. The concept is that I want to get the current time (HH: MM: SS) when I press the button. Inside the button that I did something a recurring task using NSTimer.once, I get the current time after one minitue when NSTimer sholud invalidate (nstimer calls the call (0.05f)). Here I need to calculate the pressing time with the next minute. Sorry, I'm not mistaken, I'm stupid in English. here is the code i still have ...

- (void) scanButtonTouchUpInside {


    if (check) {
        position=CGPointMake(14.0f, 7.0f);
        position1=CGPointMake(7.0f, 14.0f);
        check=NO;

       timer= [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];



    }
  else
  {
      //animation stop code here.................
      if ([timer isValid]) {
          [timer invalidate];
      }
      check=YES;
      NSLog(@"stopppppppp.....................");
//      overlayGraphicView.frame = CGRectMake(30, 100, 32, 32);
//      overlayGraphicView1.frame = CGRectMake(30, 152, 32, 32);
  }


}

-(void)onTimer
{


    overlayGraphicView.center = CGPointMake(overlayGraphicView.center.x+position.x,overlayGraphicView.center.y+position.y);

    if(overlayGraphicView.center.x > 320 || overlayGraphicView.center.x < 0)
        position.x = -position.x;
    if(overlayGraphicView.center.y > 480 || overlayGraphicView.center.y < 0)
        position.y = -position.y;



    overlayGraphicView1.center = CGPointMake(overlayGraphicView1.center.x+position1.x,overlayGraphicView1.center.y+position1.y);

    if(overlayGraphicView1.center.x > 320 || overlayGraphicView1.center.x < 0)
        position1.x = -position1.x;
    if(overlayGraphicView1.center.y > 480 || overlayGraphicView1.center.y < 0)
        position1.y = -position1.y;


}
+4
source share
2 answers

I made this simple way ...

-(void)onTimer
{


    overlayGraphicView.center = CGPointMake(overlayGraphicView.center.x+position.x,overlayGraphicView.center.y+position.y);

    if(overlayGraphicView.center.x > 320 || overlayGraphicView.center.x < 0)
        position.x = -position.x;
    if(overlayGraphicView.center.y > 480 || overlayGraphicView.center.y < 0)
        position.y = -position.y;



    overlayGraphicView1.center = CGPointMake(overlayGraphicView1.center.x+position1.x,overlayGraphicView1.center.y+position1.y);

    if(overlayGraphicView1.center.x > 320 || overlayGraphicView1.center.x < 0)
        position1.x = -position1.x;
    if(overlayGraphicView1.center.y > 480 || overlayGraphicView1.center.y < 0)
        position1.y = -position1.y;
  }
- (void) scanButtonTouchUpInside {

    [self performSelector:@selector(timerInvalidate) withObject:nil afterDelay:60.0];
    if (check) {
        position=CGPointMake(14.0f, 7.0f);
        position1=CGPointMake(7.0f, 14.0f);

       timer= [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];



    }

}

-(void)timerInvalidate
{
    if ([timer isValid]) {
        [timer invalidate];
    }
}

, https://github.com/jj0b/OverlayViewTester

+5

scanButtonTouchUpInside , timer= [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

onTimer, scanButtonTouchUpInside, [self scanButtonTouchUpInside]; .

-, ,

-(NSInteger)calculateMinutesFromLastDate:(NSDate *)lastDate toNow:(NSDate *)nowDate
    NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:nowDate];
    NSInteger nowCurrentMinute = [nowDateComponents minute];

    NSDateComponents *lastDateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:lastDate];
    NSInteger lastMinute = [lastDateComponents minute];
    NSInteger differenceMinutes = nowCurrentMinute - lastMinute;

    return differenceMinutes
}

, .

+1

Source: https://habr.com/ru/post/1548643/


All Articles