views:

140

answers:

2

I am making a drawing app and I want to increment my brush size depending on the touches velocity.

How would I accomplish this?

A: 

Well, UITouch has properties for location and timestamp; using these properties, you can calculate the "velocity" of the touch between two touch events.

This will only work for single touches, of course, and you may have to smooth the results. Also, the user might be able to "fool" you by quickly tapping with two fingers alternatingly ;-)

oefe
+2  A: 

Yes. See the methods on UIView named touches*, specifically:

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

It gets called when a move ("slide"?) is detected, and doesn't suffer from being "fooled" by multiple quick touches. You can get the coordinate of the current finger position in the view and the time the event ocurred:

UITouch *touch = [touches anyObject];
CGPoint inFrameCoordinate = [touch locationInView:self];
NSTimeInterval timestamp = [touch timestamp]

Get the distance and time between two coordinates to calculate the velocity.

calmh