添加计时器以计算时间UIButton按下iPhone

时间:2011-06-29 09:17:09

标签: iphone objective-c uibutton nstimer

如何添加计时器以检测iPhone上按下UIButton的时间?

由于

3 个答案:

答案 0 :(得分:4)

在.h文件中将buttonPressedStartTime声明为CFTimeInterval。

按下按钮时,存储当前时间:

buttonPressedStartTime = CFAbsoluteTimeGetCurrent();    

当用户收起垃圾箱时,

float deltaTimeInSeconds = CFAbsoluteTimeGetCurrent() - buttonPressedStartTime;

答案 1 :(得分: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.
 }

答案 2 :(得分: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:操作连接到“内部触摸”插座,并在接口生成器的按钮连接选项卡中将buttonUp:连接到“内部触摸”或“触摸外部”插座。