How can I track a CGPoint of a touch input in a UIView, as the finger moves along the view?

StackOverflow https://stackoverflow.com/questions/22691527

سؤال

How can I get a CGPoint of a users touch?

It's actually a MKMapView, but since it inherits from a UIView, I'm thinking it's the same.

I can add a UIGestureRecognizer. But what I'm looking to, is to track the finger's position as it moves in the map view.

Thank you!

هل كانت مفيدة؟

المحلول

Use a gesture recognizer that uses this handler:

- (void)handleGesture:(UIGestureRecognizer*)gesture
{
    CGPoint locationInView = [gesture locationInView:self.view];
    // do stuff
}

نصائح أخرى

The implementation below updates the coordinates when you drag and touches on the screen.

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint coordinate = [[touches anyObject] locationInView:self.view];
    self.coordinateLabel.text = [NSString stringWithFormat:@"%f, %f",coordinate.x,coordinate.y];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top