: UISwipeGestureRecognizer. - UITouch, touchesBegan:withEvent:. super.
When your recognizer starts its action, the recognizer will be passed as a parameter sender. You can set it for start and end touch objects, and then use the method locationInView:and property timestampto determine the scroll speed (speed = change in distance / change in time).
So, it will be something like this:
@interface DDSwipeGestureRecognizer : UISwipeGestureRecognizer
@property (nonatomic, retain) UITouch * firstTouch;
@property (nonatomic, retain) UITouch * lastTouch;
@end
@implementation DDSwipeGestureRecognizer
@synthesize firstTouch, lastTouch;
- (void) dealloc {
[firstTouch release];
[lastTouch release];
[super dealloc];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self setFirstTouch:[touches anyObject]];
[super touchesBegan:touches withEvent:event];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setLastTouch:[touches anyObject]];
[super touchesEnded:touches withEvent:event];
}
@end
Then in another place you would do:
DDSwipeGestureRecognizer *swipe = [[DDSwipeGestureRecognizer alloc] init];
[swipe setTarget:self];
[swipe setAction:@selector(swiped:)];
[myView addGestureRecognizer:swipe];
[swipe release];
And your action would be something like this:
- (void) swiped:(DDSwipeGestureRecognizer *)recognizer {
CGPoint firstPoint = [[recognizer firstTouch] locationInView:myView];
CGPoint lastPoint = [[recognizer lastTouch] locationInView:myView];
CGFloat distance = ...;
NSTimeInterval elapsedTime = [[recognizer lastTouch] timestamp] - [[recognizer firstTouch] timestamp];
CGFloat velocity = distance / elapsedTime;
NSLog(@"the velocity of the swipe was %f points per second", velocity);
}
Warning: code entered in the browser and not compiled. Warning.
source
share