4

I want to track The speed Of the Touch moved in pixel /Second
Any Budy Having idea For This

Except the PanGesture Method

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
NIKHIL
  • 2,719
  • 1
  • 26
  • 50
  • Could you clarify whether you mean speed or velocity? i.e. If the user takes exactly 1 second to draw a circle, and finishes on exactly the same pixel they started on, would you expect the answer to be a) equal to the circumference of that circle / 1 second, or b) zero. Note that the implementation for b) will prove to be a lot simpler, but will work fine for straight lines. – Dave Feb 22 '11 at 08:00
  • NO Actually I want is ==>If I Move My fingure On to the screen at that time i need the rate of change of pixel per second Which is provided in the PanGesture but Pangesture does not Supported by ios Less than 3.2 please tell if YOu have Any idea???Thank You For Reply – NIKHIL Feb 22 '11 at 11:52
  • @Dave : I Want it to move randomly in entire screen – NIKHIL Feb 22 '11 at 11:55
  • Does your app use an NSTimer scheduledTimerWithTimeInterval regular callback? Do you want AVERAGE speed over a period of time, or just the speed of each movement detected? I'm just trying to figure out how to best help answer your question. – Dave Feb 22 '11 at 13:36
  • i need the speed of each movement if u look at pan gesture library there will be a function - (CGPoint)velocityInView:(UIView *)view; i want the out put like that as this method return the cgpoint which is change in point per second – NIKHIL Feb 22 '11 at 14:29

1 Answers1

11

I'll assume you want to know the velocity between consecutive movements, and not an average over a series of movements. The theory should be easy enough to adapt if you have other requirements, though.

You will need to know three things:

  1. Where is the touch now?
  2. Where was the touch previously?
  3. How much time has passed since then?

Then just use Pythagoras to get your distance, and divide by time for speed.

When you get a touchesMoved event, it will provide the current and previous position for you. All you need to then add is a measure of time.

For that, you will want an NSDate property in your class to help calculate the time interval. You could initialise and release it in viewDidLoad / viewDidUnload, or somewhere similar. In my example, mine is called lastTouchTime, it is retained, and I initialised like this:

self.lastTouchTime = [NSDate date];

and I release it in the predictable way.

Your touchesMoved event should then look something like this:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{       

    NSArray *touchesArray = [touches allObjects];
    UITouch *touch;
    CGPoint ptTouch;
    CGPoint ptPrevious;

    NSDate *now = [[NSDate alloc] init];
    NSTimeInterval interval = [now timeIntervalSinceDate:lastTouchTime];
    [lastTouchTime release];
    lastTouchTime = [[NSDate alloc] init];
    [now release];

    touch = [touchesArray objectAtIndex:0];
    ptTouch = [touch locationInView:self.view];
    ptPrevious = [touch previousLocationInView:self.view];

    CGFloat xMove = ptTouch.x - ptPrevious.x;
    CGFloat yMove = ptTouch.y - ptPrevious.y;
    CGFloat distance = sqrt ((xMove * xMove) + (yMove * yMove));

    NSLog (@"TimeInterval:%f", interval);
    NSLog (@"Move:%5.2f, %5.2f", xMove, yMove);
    NSLog (@"Distance:%5.2f", distance);
    NSLog (@"Speed:%5.2f", distance / interval);
}

Apologies if I've made a faux pas with any memory management, I'm still not very comfortable with ObjectiveC's way of doing things. I'm sure somebody can correct it if necessary!

Good luck, Freezing.

Dave
  • 3,438
  • 20
  • 13