Measure the start and end time, and calculate the difference in the UIGestureRecognizer
callback method for gestureRecognizer.state == UIGestureRecognizerStateBegan
and gestureRecognizer.state == UIGestureRecognizerStateEnded
get timespan of swipe action in iPhone app
-
25-06-2023 - |
Question
How can we get the time a user spent to finish a swipe right or swipe left action? Already googled but no luck. If there is not built-in solution already, will touch begin and touch end together work?
Solution
OTHER TIPS
You can do this by getting the date (which you create as ivar or property) in the touchesBegan method like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
touchStartDate = [NSDate date];
}
Then you fetch the difference in the touches end method:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSTimeInterval *touchDuration = [[NSDate date] timeIntervalSinceDate:touchStartDate];
NSLog(@"Touchduration %f",touchDuration);
}
If you only want to get the duration of a swipe left/right gesture you also need to check if the x value of the touch changed. This should be easy.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow