回答
在.h文件中聲明buttonPressedStartTime作爲CFTimeInterval。
當按下按鈕,保存當前時間:
buttonPressedStartTime = CFAbsoluteTimeGetCurrent();
當用戶thouches了冰兒,
float deltaTimeInSeconds = CFAbsoluteTimeGetCurrent() - buttonPressedStartTime;
謝謝!但它給我結果=「0.000000」 – LightNight
對不起,我的錯= =工作很好! =) – LightNight
- (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.
}
感謝您的回覆。但這不是我想要的。我不需要點擊按鈕數。如果用戶觸摸按鈕並握住按鈕,那麼我需要時間來控制他的握持。 – LightNight
試試這個:
// 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」或在界面構建器的按鈕連接選項卡中的「觸摸外部」插座。
- 1. 計算UIButton按下持續時間
- 2. 使用UIButton添加和減少時間到計時器
- 3. 添加計時器到iPhone
- 4. 計時器 - 計算時間
- 5. iPhone SDK,計算給定時間之間的時間差
- 6. 可以按時添加的倒計時器的算法
- 7. iPhone - 在按下時抓住UIButton事件
- 8. Allegro定時器如何計算時間?
- 9. 添加時間加上並重新計算日期時間
- 10. 按天計算POSIXlt時間
- 11. 計算時間差按鈕
- 12. 添加時間到定時器/計數器
- 13. IOS - 讓時的UIButton按下
- 14. 添加setUTCDate()來代替「計算機」時間
- 15. 計算「按時履約」計劃時間和實際時間
- 16. 當UIButton保持按下時添加額外的圖像
- 17. 計算當前時間和未來時間之間的時差
- 18. 按小時計算加班
- 19. 如何計算添加特定項目的時間?
- 20. Android:時間計算,如何添加指定的分鐘數
- 21. 計算時間(到下一個時間段的整數時間)
- 22. 如何在計時器中計算所計算的時間?
- 23. 計算按鍵在鍵盤上按下的時間長度
- 24. Android Studio - 通過在設定時間內添加小時來計算完成時間
- 25. 按下UIButton時移動
- 26. UIButton按下時閃爍
- 27. 按下時重繪UIButton
- 28. 按下按鍵時使用計時器
- 29. 在Java中添加時間計數器?
- 30. 如何添加時間加上並重新計算的日期時間
你不需要計時器。只需一個簡單的變量來增加抽頭的數量。 – Eimantas
我想不..我只是想顯示用戶按了多長時間按鈕。 – LightNight