2011-06-29 32 views

回答

4

在.h文件中聲明buttonPressedStartTime作爲CFTimeInterval。

當按下按鈕,保存當前時間:

buttonPressedStartTime = CFAbsoluteTimeGetCurrent();  

當用戶thouches了冰兒,

float deltaTimeInSeconds = CFAbsoluteTimeGetCurrent() - buttonPressedStartTime; 
+0

謝謝!但它給我結果=「0.000000」 – LightNight

+0

對不起,我的錯= =工作很好! =) – LightNight

1
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     //Check here for touch is in your button frame than add 
    [self performSelector:@selector(judgeLongPresstime) withObject:nil afterDelay:YourDelayTimeCheck]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [NSObject cancelPreviousPerformRequestWithTarget:self]; 
} 

-(void)judgeLongPresstime 
{ 
     //Add timer here and get time of increaseing. 
} 
+0

感謝您的回覆。但這不是我想要的。我不需要點擊按鈕數。如果用戶觸摸按鈕並握住按鈕,那麼我需要時間來控制他的握持。 – LightNight

1

試試這個:

// somewhere in interface 
NSDate *_startDate; 


- (IBAction)buttonDown:(id)sender { 
    _startDate = [[NSDate date] retain]; 
} 


- (IBAction)buttonUp:(id)sender { 
    NSTimeInterval *pressedForInSeconds = [[NSDate date] timeIntervalSince1970] - [startDate timeIntervalSince1970]; 
    NSLog(@"button was pressed for: %d seconds", pressedForInSeconds); 
    [_startDate release]; 
} 

然後線buttonDown: 「Touch inside inside」插座,buttonUp:「Touch up inside」或在界面構建器的按鈕連接選項卡中的「觸摸外部」插座。