2010-12-15 4 views

Répondre

13

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

} 
+6

Ne pas oublier d'ajouter #import pour déposer DragGestureRecognizer.h – HotJard

1

Si vous écrivez votre propre UIGestureRecognizer vous pouvez obtenir les objets tactiles remplaçant:

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

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

Le Apple docs a beaucoup d'informations sur le sous-classement

2

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

6

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

INITIALISER cible:

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

Poignée UIGestureRecognizerStateChanged pour obtenir des changements d'emplacement:

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

Ce n » t obtenir 'UITouch', il obtient seulement un' CGPoint' pour le cadre local de la vue spécifique. – Chris

0

Voici une méthode pour obtenir un appui long ajouté à un UIView arbitraire. Ceci vous permet d'exécuter une pression longue sur n'importe quel nombre d'UIViews. Dans cet exemple, je limite l'accès à la méthode UIView par le biais de balises, mais vous pouvez également utiliser isKindOfClass.

(D'après ce que je l'ai trouvé, vous devez utiliser touchesMoved pour LongPress parce que les feux touchesBegan avant la LongPress devient actif)

sous-classe - .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-classe - .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 
        } 
      } 
-1

Simple et rapide:

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

Où est votre reconnaisseur UIGestureRecognizer

+2

iOS 8 [ valueForUndefinedKey:]: cette classe n'est pas compatible avec les valeurs de clé pour les touches. ' –

Questions connexes