Reputation: 1470
Im sorry if this is a simple question for you.But i stuck here.
I have added a pan gesture to the view.
I stuck with a point where i want to get the time interval, when user pan forward and backward in a view.
Now i used this code for getting the time interval,
if ([sender state] == UIGestureRecognizerStateBegan )
{
startTime = [[NSDate alloc]init];}
else if([sender state] == UIGestureRecognizerStateChanged )
{
CGPoint velocity = [sender velocityInView:self.imageView];
if(velocity.x > 0)
{
NSLog(@"gesture went right");
}
else
{
NSLog(@"gesture went left");
}
double chagedTime = [startTime timeIntervalSinceNow] * -1000.0;
NSLog(@"%f changed ",chagedTime);
}
Now I'm getting the time interval when user start to pan forward, but the problem is suppose when user start to pan backward the time interval is also increasing.
So how can i time interval increasing while panning forward and time interval decrease while panning backward.
Hoping for your help.
Thanks in advance.
Upvotes: 0
Views: 104
Reputation: 4797
If I'm reading your question correctly, you want to record the amount of time a user spends panning forward, and subtract from that the amount of time a user spends panning backwards.
//Declared:
double netTimeInterval;
double previousStartTime;
if ([sender state] == UIGestureRecognizerStateBegan )
netTimeInterval = 0;
else if([sender state] == UIGestureRecognizerStateChanged )
{
CGPoint velocity = [sender velocityInView:sender.view];
double changedTime;
changedTime = (velocity.x > 0)? (CFAbsoluteTimeGetCurrent()-previousStartTime):(previousStartTime-CFAbsoluteTimeGetCurrent());
//Conversion from seconds to microseconds
changedTime *=1000;
netTimeInterval += changedTime;
NSLog(@"%f",changedTime);
NSLog(@"%f",netTimeInterval);
}
previousStartTime = CFAbsoluteTimeGetCurrent();
Upvotes: 1