2014-07-16 72 views
4

嗨,我是一种新的iOS.Now我混淆了这个概念..概念是,我想获得当前时间(HH:MM:SS)时我点击button.Inside按钮我做了一些重复的任务,通过使用NSTimer。一次我得到当前时间NSTimer sholud失效后(nstimer是每个调用(0.05f))。在这里,我需要计算按下时间与下一个minitue.thanks advance.sorry不要把我误认为我是愚蠢的英语。 这里是我现在还在代码..从下一个计算当前时间然后NSTimer不应该工作

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


} 

回答

5

我已经这样做了简单的方法...

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

1

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

希望它可以帮助你。

+0

采取覆盖源? –

+0

感谢@nathan hegedus我以不同的方式完成了这项工作 – karthikeyan