2

您如何记录触摸的持续时间并在屏幕上打印?

在此处输入图像描述

这是上面的代码:

// add this ivar to your view controller
NSTimeInterval lastTouch;
int textTime;

// assign the time interval in touchesBegan:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
    lastTouch = [event timestamp];
}


// calculate and print interval in touchesEnded:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
    NSTimeInterval touchBeginEndInterval = [event timestamp] - lastTouch;
    textTime = touchBeginEndInterval;
    NSLog(@"touchBeginEndInterval %f", touchBeginEndInterval);
    dynLabel.text = textTime;
}
4

1 回答 1

1
dynLabel.text = textTime;

标签的text属性需要 a NSString*,但你给它一个NSTimeIntervalIe 一个数字。这就是错误的意思。-stringWithFormat:使用例如NSString 的方法创建一个字符串。

于 2012-10-05T00:20:42.983 回答