Getting the UITouch objects for a UIGestureRecognizer Getting the UITouch objects for a UIGestureRecognizer ios ios

Getting the UITouch objects for a UIGestureRecognizer


Jay's right... you'll want a subclass. Try this one for size, it's from one of my projects. In DragGestureRecognizer.h:

@interface DragGestureRecognizer : UILongPressGestureRecognizer {}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;@end@protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;@end

And in DragGestureRecognizer.m:

#import "DragGestureRecognizer.h"@implementation DragGestureRecognizer- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesMoved:touches withEvent:event];    if ([self.delegate respondsToSelector:@selector(gestureRecognizer:movedWithTouches:andEvent:)]) {        [(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];    }}@end 

Of course, you'll need to implement the

- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event; 

method in your delegate -- for example:

DragGestureRecognizer * gr = [[DragGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];gr.minimumPressDuration = 0.15;gr.delegate = self;[self.view addGestureRecognizer:gr];[gr release];- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event{    UITouch * touch = [touches anyObject];    self.mTouchPoint = [touch locationInView:self.view];    self.mFingerCount = [touches count];}


If it is just the location you are interested in, you do not have to subclass, you will be notified of location changes of the tap from the UIGestureRecognizer.

Initialize with target:

self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];[self.tableView addGestureRecognizer: longPressGestureRecognizer];

Handle UIGestureRecognizerStateChanged to get location changes:

- (void)handleGesture: (UIGestureRecognizer *)theGestureRecognizer {    switch (theGestureRecognizer.state) {        case UIGestureRecognizerStateBegan:        case UIGestureRecognizerStateChanged:        {            CGPoint location = [theGestureRecognizer locationInView: self.tableView];            [self infoForLocation: location];            break;        }        case UIGestureRecognizerStateEnded:        {            NSLog(@"Ended");            break;        }        default:            break;    }}


If you only need to find out the location of the gesture, you can call either locationInView: or locationOfTouch:inView: on the UIGestureRecognizer object. However if you want to do anything else, then you'll need to subclass.