2011-11-25 6 views
10
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"touchesBegan"); 

    //test 
    UITouch *touch = [event allTouches] anyObject]; 
    if ([touch tapCount] == 2) { 
     NSLog (@"tapcount 2"); 
     [self.textview becomeFirstResponder]; 

    } 

    else if ([touch tapCount] == 1) { 
     NSLog (@"tapcount 1"); 
     [self.textview becomeFirstResponder]; 
     [self.view performSelector:@selector(select:)]; 


    } 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesBegan:touches withEvent:event]; 
    NSLog(@"touchesMoved"); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    NSLog(@"****touchesEnded"); 
    [self.nextResponder touchesEnded: touches withEvent:event]; 
    NSLog(@"****touchesEnded"); 
    [super touchesEnded:touches withEvent:event]; 
    NSLog(@"****touchesEnded"); 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesCancelled:touches withEvent:event]; 
    NSLog(@"touchesCancelled"); 
} 

MI PREGUNTA:ios ¿Cómo hacer que UITextView detecte un toque?

Quiero simular dos grifos al tocar una vez en un UITextView, que es TextView en este código. Pero no obtengo NSLog de uno y dos toques cuando toco una o dos veces en la vista de texto, solo fuera de ella. ¿Qué debo hacer para que funcione?

+0

Antes que nada, este código tendría que estar en una subclase personalizada de UITextView para funcionar, y probablemente interferiría con el funcionamiento normal. En segundo lugar, ¿a qué te refieres con "quiero simular dos pulsaciones cuando toco una vez en una UITextView", quiero decir con qué propósito? – NJones

+0

@NJones Quiero dar a los usuarios la opción de elegir entre uno o dos toques para una determinada función. Quiero simular dos toques para obtener el texto seleccionado y hacer solo un toque para los usuarios si eligen esta opción. ¿Es posible forzar dos pulsaciones al tocar una vez en UITextView? La solución de Bensnider funciona perfectamente. Pero, ¿cómo simular dos golpes? – wagashi

Respuesta

16

Probablemente usaría dos gesture recognizers aquí.

//...some stuff above here probably in you're controllers viewDidLoad 

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapRecognized:)]; 
singleTap.numberOfTapsRequired = 1; 
[someTextView addGestureRecognizer:singleTap]; 
[singleTap release]; 

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapRecognized:)]; 
doubleTap.numberOfTapsRequired = 2; 
[someTextView addGestureRecognizer:doubleTap]; 
[doubleTap release]; 

Y los selectores sólo sería como:

- (void)singleTapRecognized:(UIGestureRecognizer *)gestureRecognizer { 
    NSLog(@"single tap"); 
    // ...etc 
} 

- (void)doubleTapRecognized:(UIGestureRecognizer *)gestureRecognizer { 
    NSLog(@"double tap"); 
    // ...etc 
} 
+0

Su solución funciona de forma expectante. Sin embargo, realmente quiero simular dos toques al tocar una vez para obtener el texto seleccionado. – wagashi

+0

¿A qué te refieres con, obtener el texto seleccionado? – NJones

+0

La siguiente pregunta es, ¿cómo hacer esto mientras permite que la vista de texto detecte los grifos en los enlaces? –

1

que tenía el mismo problema y las otras respuestas no funcionó para mí. Entonces esto es lo que hice.

Adjunté el gesto como si fuera otra respuesta sugerida. Luego me aseguré de que se llamara el método delegado para la selección. Intenté simplemente seleccionar la celda pero eso no desencadenó el método delegado. Creo que solo las interacciones del usuario hacen que se llame al método de delegado, por lo que este código imita ese comportamiento.

https://gist.github.com/brennanMKE/e89bf7a28d96812d6a22

@implementation TappableTextView 

- (instancetype)init { 
    self = [super init]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 

- (instancetype)initWithCoder:(NSCoder *)coder { 
    self = [super initWithCoder:coder]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 

- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 

- (void)setup { 
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapRecognized:)]; 
    singleTap.numberOfTapsRequired = 1; 
    [self addGestureRecognizer:singleTap]; 
} 

- (void)singleTapRecognized:(id)sender { 
    UIView *superview = self.superview; 

    UICollectionViewCell *cell = nil; 
    NSIndexPath *indexPath = nil; 

    while (superview) { 
     if ([superview isKindOfClass:[UICollectionViewCell class]]) { 
      cell = (UICollectionViewCell *)superview; 
     } 

     if ([superview isKindOfClass:[UICollectionView class]] && cell) { 
      UICollectionView *collectionView = (UICollectionView *)superview; 
      indexPath = [collectionView indexPathForCell:cell]; 
      NSAssert(collectionView.delegate, @"Delegate must be defined"); 
      NSAssert([collectionView.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)], @"Selection must be supported"); 
      if (indexPath && [collectionView.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) { 
       [collectionView.delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath]; 
      } 

      return; 
     } 

     superview = superview.superview; 
    } 
} 

@end 
0

me encontré con un problema similar donde quería capturar un grifo en un Textview sin editar el texto subyacente.

Esta respuesta es para Swift 3.0.

Para mi solución, implementé el método textView delegado textViewShouldBeginEditing y devolví falso para el valor. Esto me permite capturar los toques en el textView sin ningún gasto adicional.

extension ViewController: UITextViewDelegate { 
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { 
     // Do something 

     return false 
    } 
} 

Solo asegúrese de asignar textView para usar el delegado en su clase ViewController.

Cuestiones relacionadas