2012-01-05 16 views
12

Tengo una UIView con varias subvistas y un gesto Tap reconocido como asociado y quiero imitarlo teniendo un efecto de 'toque'. Es decir, cuando ocurre el tap, quiero mostrar la vista de contenedor para tener un color de fondo diferente y el texto de cualquier subvista UILabels para que también se vea resaltado.Destacando una UIView similar a UIButton

Cuando recibo el caso del grifo de UITapGestureRecognizer, que puede cambiar el color de fondo muy bien e incluso establecer el UILabel a [label setHighlighted:YES];

Por diversas razones, no puedo cambiar el UIView a uicontrol.

Pero si agrego algo de UIViewAnimation para revertir el resaltado, no ocurre nada. ¿Alguna sugerencia?

- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture { 
     [label setHighlighted:YES]; // change the label highlight property 

[UIView animateWithDuration:0.20 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseIn 
        animations:^{ 
         [containerView setBackgroundColor:originalBgColor];   
         [label setHighlighted:NO]; // Problem: don't see the highlight reverted 
        } completion:^(BOOL finished) {       
         // nothing to handle here 
        }];  
} 
+0

¿Por qué no lo convierten en un 'UIButton'? –

+0

Porque no es una base de código que tengo y hay otras dependencias, así que tengo que dejarlo como UIView. –

+0

Eche un vistazo a esta biblioteca: https://github.com/mta452/UIView-TouchHighlighting –

Respuesta

6

setHighlighted no es una propiedad de vista animable. Además, estás diciendo dos cosas opuestas: establecisteHighlighted en YES y NO en el mismo aliento. El resultado será que no pasa nada porque no hay un cambio general.

Utilice el controlador de finalización o el rendimiento retrasado para cambiar el resaltado más tarde.

EDIT:

se dice "Intentamos tanto pero tampoco trabajado." Tal vez necesites una aclaración sobre lo que quiero decir con un rendimiento retrasado. Acabo de intentar esto y funciona perfectamente:

- (void) tapped: (UIGestureRecognizer*) g { 
    label.highlighted = YES; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     label.highlighted = NO; 
    }); 
} 

La etiqueta debe tener un diferente textColor vs su highlightedTextColor da la circunstancia de que algo visible.

+0

Intenté ambas cosas, pero ninguna de ellas funcionó .. Tal vez tenga que encontrar otra forma creativa de hacerlo o volver a conectar la base de código existente a usa UIControl. –

0

solución simple es anulación del grifo gesto regognizer , como a continuación:

4.x Swift

class TapGestureRecognizer: UITapGestureRecognizer { 
    var highlightOnTouch = true 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { 
     super.touchesBegan(touches, with: event) 

     if highlightOnTouch { 
      let bgcolor = view?.backgroundColor 
      UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: { 
       self.view?.backgroundColor = .lightGray 
      }) { (_) in 
       UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: { 
        self.view?.backgroundColor = bgcolor 
       }) 
      } 
     } 
    } 

} 
Cuestiones relacionadas