记录触摸的持续时间,然后打印

时间:2012-10-04 23:01:09

标签: iphone xcode

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

enter image description here

以下是上面的代码:

// 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;
}

1 个答案:

答案 0 :(得分:1)

dynLabel.text = textTime;

标签的text属性需要NSString*,但是你给它一个NSTimeInterval I.e。一个号码。这就是错误的含义。使用例如创建字符串NSString的-stringWithFormat:方法。