ios - 从下一个 minitue 计算当前时间然后 NSTimer 不应该工作

标签 ios iphone nstimer

嗨,我是 iOS 的新手。现在我对这个概念感到困惑。这个概念是我想在单击按钮时获取当前时间 (HH:MM:SS)。在按钮内我已经完成了通过使用 NSTimer 重复任务。一旦我在一分钟后得到当前时间,NSTimer sholud 将失效(nstimer 调用每个(0.05f))。这里我需要计算下一个 minitue 的按下时间.thanks in advance.sorry don't mistake me I is stupid in English. 这是我现在还在的代码..

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


}

最佳答案

首先 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
}

希望对你有帮助。

关于ios - 从下一个 minitue 计算当前时间然后 NSTimer 不应该工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24782161/

相关文章:

没有开发者帐户或 adhoc 的 iOS 应用程序

ios - 如何在不同的 iphone/ipad 屏幕尺寸下预览 Main.storyboard?

ios - 如何在用户在 Swift 中键入时调整 UITextField 的大小

ios - 自动旋转代码适用于 iPhone,但不适用于 iPad

ios - 当应用程序后台运行时,NSTimer 是否会触发?

ios - UILabel 在运行时为零,在模拟器上工作,但在设备上崩溃

ios - 显示YouTube channel

ios - 将数据加载到 Metal 中的 3D 纹理中

iphone - 如何检测 UISearchBar/UITextField 的输入暂停?

iPhone:当iPhone锁定时如何保持定时器唤醒?