2014-07-16 55 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