2010-12-15 19 views

Respuesta

13

Jay tiene razón ... querrás una subclase. Prueba este tamaño, es de uno de mis proyectos. 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 

Y en 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 

Por supuesto, tendrá que poner en práctica el método

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

en su delegado - por ejemplo, :

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

No se olvide de añadir import DragGestureRecognizer.h a presentar – HotJard

1

Si va a escribir su propio UIGestureRecognizer puede obtener los objetos táctiles anulando:

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

o el equivalente trasladó, terminó o se cancela

El Apple docs dispone de mucha información sobre la subclasificación

2

Si solo necesita averiguar la ubicación del gesto, puede llamar a locationInView: o locationOfTouch: inView: en el objeto UIGestureRecognizer. Sin embargo, si quieres hacer otra cosa, necesitarás una subclase.

6

Si solo es la ubicación que le interesa, no es necesario que tenga una subclase, se le notificarán los cambios de ubicación del tap desde el UIGestureRecognizer.

Inicializar con el objetivo de:

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

manija UIGestureRecognizerStateChanged para conseguir cambios de ubicación:

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

Esto no tiene' Obtenga 'UITouch', solo obtiene un' CGPoint' para el marco local de la vista específica. – Chris

0

Aquí es un método para obtener una pulsación larga añade a un UIView arbitraria.

Esto le permite ejecutar longpress en cualquier cantidad de UIViews. En este ejemplo, restrinjo el acceso al método UIView mediante etiquetas, pero también podría usar isKindOfClass.

(Por lo que he encontrado que hay que utilizar para touchesMoved LongPress porque los fuegos touchesBegan antes de la LongPress se convierte en activo)

subclase - .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 

subclase - .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 y rápido:

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

Dónde reconocedor es su UIGestureRecognizer

+2

iOS 8 [ valueForUndefinedKey:]: esta clase no es un valor clave que cumpla con la codificación para los toques de tecla. ' –

Cuestiones relacionadas