[英]Adding timer to count time UIButton pressed iPhone
如何添加計時器來檢測在 iPhone 上按下 UIButton 的時間?
謝謝
在 your.h 文件中將 buttonPressedStartTime 聲明為 CFTimeInterval。
按下按鈕時,存儲當前時間:
buttonPressedStartTime = CFAbsoluteTimeGetCurrent();
當用戶開始狂歡時,
float deltaTimeInSeconds = CFAbsoluteTimeGetCurrent() - buttonPressedStartTime;
- (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.
}
嘗試這個:
// 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 down inside”插座,並將buttonUp:
連接到 Interface Builder 中按鈕連接選項卡中的“Touch up inside”或“Touch up outside”插座。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.