0

我是IOS开发的新手,有一点java和Arduino编程经验。我正在做一个简单的莫尔斯电码项目。如何跟踪在 iOS 中按下按钮的时间量?我是否只需要一个大循环来不断扫描被按下的按钮?

4

2 回答 2

3

我会做这样的事情:

向您的班级添加一个属性以存储最近一次着陆的日期。最好在.m-file 的类扩展中。

@interface YourViewController ()
@property (nonatomic, strong) NSDate *buttonTouchDownDate;
@end

以编程方式或使用界面生成器将您的按钮连接到Touch DownTouch Up Inside控制事件。这在此处进行了详细描述。

然后存储按下按钮的日期。当手指抬起时,计算存储日期与现在之间的时间间隔。

- (IBAction)buttonDidTouchDown:(id)sender
{
    self.buttonTouchDownDate = [NSDate date];
}
- (IBAction)buttonDidTouchUp:(id)sender
{
    // Will return a negative value, so we use the ABS-macro.
    NSTimeInterval timeInterval = ABS([self.buttonTouchDownDate timeIntervalSinceNow]); 
    NSLog(@"Time interval: %f", timeInterval);    
}
于 2013-08-17T15:07:49.337 回答
0

您必须结合 2 个函数结果:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

在那里你得到开始时间。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  if (self.touchInside) {
    NSLog(@"TouchUpInside");
  } else {
    NSLog(@"TouchUpOutside");
  }
}

如果一个touchupinside比你得到“结束时间”并计算持续时间。

于 2013-08-17T14:40:28.923 回答