[英]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.