iphone - Implement velocity in custom UIGestureRecognizer -
i have written custom uigesturerecognizer handles rotations 1 finger. designed work apples uirotationgesturerecognizer , return same values does.
now, implement velocity cannot figure out how apple defines , calculates velocity gesture recognizer. have idea how apple implements in uirotationgesturerecognizer?
you have keep reference of last touch position , it's timestamp.
double last_timestamp; cgpoint last_position;
then like:
-(void) touchesbegan:(nsset *)touches withevent:(uievent *)event{ last_timestamp = cfabsolutetimegetcurrent(); uitouch *atouch = [touches anyobject]; last_position = [atouch locationinview: self]; } -(void) touchesmoved:(nsset *)touches withevent:(uievent *)event { double current_time = cfabsolutetimegetcurrent(); double elapsed_time = current_time - last_timestamp; last_timestamp = current_time; uitouch *atouch = [touches anyobject]; cgpoint location = [atouch locationinview:self.superview]; cgfloat dx = location.x - last_position.x; cgfloat dy = location.y - last_position.y; cgfloat path_travelled = sqrt(dx*dx+dy*dy); cgfloat sime_kind_of_velocity = path_travelled/elapsed_time; nslog (@"v=%.2f", sime_kind_of_velocity); last_position = location; }
this should give kind of speed reference.
Comments
Post a Comment