Comrod33
Comrod33

Reputation: 161

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. 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.

Upvotes: 0

Views: 221

Answers (1)

Jano
Jano

Reputation: 63667

Add this code to your 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);
}

Upvotes: 3

Related Questions