2

Is it possible to get a UITouch object through UIPanGestureRecognizer? How could I do?

Massimo Piazza
  • 663
  • 3
  • 7
  • 21

2 Answers2

1

As Apple documentation says, you haven't a property to get the touch in an UIGestureRecognizer object. But you can subclass the UIGestureRecognizer class, in order to override touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent: and so on, retrieving so the UITouch objects.

If can be useful, have also a look to locationOfTouch:inView:.

Cheers!

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
1

I solved the issue:

PanGestureRecognizer.h file

#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

@interface PanGestureRecognizer : UIPanGestureRecognizer {

}

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

@end

@protocol PanGestureRecognizer <UIGestureRecognizerDelegate>

- (void) panGestureRecognizer:(UIPanGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;


@end

PanGestureRecognizer.m file

#import "PanGestureRecognizer.h"


@implementation PanGestureRecognizer

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

    [super touchesMoved:touches withEvent:event];

    if ([self.delegate respondsToSelector:@selector(panGestureRecognizer:movedWithTouches:andEvent:)]) {
        [(id)self.delegate panGestureRecognizer:self movedWithTouches:touches andEvent:event];
    }

}

@end

GestureView.m file

- (void)awakeFromNib {
        PanGestureRecognizer *panRecognizer = [[PanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognition:)];
        panRecognizer.maximumNumberOfTouches = 1;
        [panRecognizer setDelaysTouchesBegan:NO];
        [panRecognizer setDelaysTouchesEnded:NO];
        [panRecognizer setCancelsTouchesInView:NO];
        panRecognizer.delegate = self;
        [self.keyboardLayoutView addGestureRecognizer:panRecognizer];
}

- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panRecognizer movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event{

}
Massimo Piazza
  • 663
  • 3
  • 7
  • 21