嗨,我是 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/