Obtention des objects UITouch pour un UIGestureRecognizer

Y at-il un moyen d'get les objects UITouch associés à un geste? UIGestureRecognizer ne semble avoir aucune méthode pour cela.

Jay a raison … vous aurez besoin d'une sous-class. Essayez celui-ci pour la taille, c'est d'un de mes projets. Dans 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 

Et dans 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 

Bien sûr, vous devrez mettre en œuvre le

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

méthode dans votre délégué – par exemple:

 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]; } 

Si ce n'est que l'location qui vous intéresse, vous n'avez pas à sous-classr, vous serez informé des changements d'location du robinet à partir du UIGestureRecognizer.

Initialiser avec la cible:

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

Gérez UIGestureRecognizerStateChanged pour get les modifications d'location:

 - (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; } } 

Si vous avez seulement besoin de connaître l'location du geste, vous pouvez appeler soit locationInView: soit locationOfTouch: inView: sur l'object UIGestureRecognizer. Cependant, si vous voulez faire autre chose, vous devrez sous-classr.

Si vous écrivez votre propre UIGestureRecognizer, vous pouvez get la substitution des objects tactiles:

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

ou l'équivalent déplacé, terminé ou annulé

Les docs Apple ont beaucoup d'informations sur le sous-classment

Voici une méthode pour get une presse longue ajoutée à un UIView arbitraire.

Cela vous permet d'exécuter longpress sur n'importe quel nombre d'UIViews. Dans cet exemple, je restreins l'access à la méthode UIView par le biais de balises, mais vous pouvez aussi utiliser isKindOfClass.

(D'après ce que j'ai trouvé, vous devez utiliser touchesMoved pour LongPress parce que touchesBegan se triggers avant que le LongPress ne devienne actif)

sous-class – .h

  #import <UIKit/UIKit.h> #import <UIKit/UIGestureRecognizerSubclass.h> @interface MyLongPressGestureRecognizer : UILongPressGestureRecognizer - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; @property (nonatomic) CGPoint touchPoint; @property (strong, nonatomic) UIView* touchView; @end 

sous-class – .m

  #import "MyLongPress.h" @implementation MyLongPressGestureRecognizer - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; { UITouch * touch = [touches anyObject]; self.touchPoint = [touch locationInView:self.view]; self.touchView = [self.view hitTest:[touch locationInView:self.view] withEvent:event]; } #pragma mark - Setters -(void) setTouchPoint:(CGPoint)touchPoint { _touchPoint =touchPoint; } -(void) setTouchView:(UIView*)touchView { _touchView=touchView; } @end 

viewcontroller – .h

  //nothing special here 

viewcontroller – .m

  #import "ViewController.h" //LOAD #import "MyLongPress.h" @interface ViewController () //lOAD @property (strong, nonatomic) MyLongPressGestureRecognizer* longPressGesture; @end @implementation PDViewController - (void)viewDidLoad { [super viewDidLoad]; //LOAD self.longPressGesture =[[MyLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; self.longPressGesture.minimumPressDuration = 1.2; [[self view] addGestureRecognizer:self.longPressGesture]; } //LOAD -(void) setLongPressGesture:(MyLongPressGestureRecognizer *)longPressGesture { _longPressGesture = longPressGesture; } - (void)longPressHandler:(MyLongPressGestureRecognizer *)recognizer { if (self.longPressGesture.touchView.tag >= 100) /* arbitrary method for limiting tap */ { //code goes here } } 

Simple et rapide:

 NSArray *touches = [recognizer valueForKey:@"touches"]; 

UIGestureRecognizer est votre UIGestureRecognizer