UITapGestureRecognizer em UILabels na subvisão do UIScrollView não funciona
Eu tenho um problema onde o meu UITapGestureRecognizer no meu UILabels em uma exibição de conteúdo no meu UIScrollView não está chamando seus métodos.
A hierarquia de visualizações é a seguinte:
scrollView (UIScrollView)contentView (UIView)testLabel (UILabel) - aqui é onde o UITapGestureRecognizer está conectadoEu destilei o código para um exemplo para destacar o problema
// Set scrollview size - Added in Storyboad
[scrollView setContentSize:CGSizeMake([arrayOfVerbs count]*self.view.frame.size.width, scrollView.contentSize.height)];
[scrollView setCanCancelContentTouches:YES]; // Tried both yes and no
[scrollView setPagingEnabled:YES];
// Add content view
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];
[scrollView addSubview:contentView];
// Add test UILabel
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[testLabel setBackgroundColor:[UIColor redColor]];
[testLabel setText:@"Test touch"];
[testLabel setUserInteractionEnabled:YES];
[contentView addSubview:testLabel];
// Add gesture recogniser
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playSound:)];
singleTap.numberOfTapsRequired = 1;
[testLabel addGestureRecognizer:singleTap];
E este é o método que o reconhecedor de gestos de toque deve chamar
- (void)playSound:(UITapGestureRecognizer *)sender {
NSLog(@"play sound");
if(sender.state == UIGestureRecognizerStateEnded)
{
int pronounNumber = [sender.view tag];
int exampleNumber = (int)sender.view.frame.origin.x%(int)self.view.frame.size.width;
NSLog(@"Pronoun is %i and example is %i", pronounNumber, exampleNumber);
}
}
Este método nunca é chamado quando tento tocar no UILabel.
Tentei definir a propriedade canCancelContentTouches para YES e NO na visualização de rolagem, conforme sugerido por estefio, mas ainda não está funcionando.
O estranho é que, se eu adicionar um UILabel fora do scrollView, o reconhecedor de gestos funciona! Portanto, o problema ocorre apenas no meu contentView, que é uma subvisão do meu scrollView.
Eu estou usando o layout automático, se isso pode ser diferente?
Obrigado!