简体   繁体   English

如何检测手指已经触摸屏幕多长时间了iOS

[英]How to detect how long a finger has been touching the screen iOS

How exactly do I detect how long a finger has been touching the screen with iOS and Sprite Kit. 使用iOS和Sprite Kit,我如何准确地检测到手指已经触摸屏幕多长时间了。 I would like to get the amount of time from the first touch of the finger to the release of it, but I don't know how. 我想要从手指的第一次触摸到松开手指的时间,但我不知道如何。

Add this code to your UIView: 将此代码添加到您的UIView:

NSDate *startTime; 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    startTime = [NSDate date];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];  
    NSLog(@"Elapsed time: %f", -elapsedTime);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM