Tengo una vista de desplazamiento horizontal llena con UIImageViews
.
Quiero detectar un toque en UIImageView
y UIImageView
su color de background.
De alguna manera, el gesto del grifo no funciona.
Sin embargo, cuando agrego un gesto de toque a la vista de desplazamiento, funciona. El scrollview.background color
se puede cambiar.
¡Pero quiero detectar un toque en UIImageViews
que contiene!
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 127)]; [scrollView setScrollEnabled:YES]; scrollView.backgroundColor = [UIColor orangeColor]; [scrollView setShowsHorizontalScrollIndicator:NO]; UIImageView *contentOfScrollView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 1130, 125)]; scrollView.contentSize = CGSizeMake(contentOfScrollView.frame.size.width, contentOfScrollView.frame.size.height); for (int aantal=0; aantal < 6; aantal++) { UIImageView *item = [[UIImageView alloc] initWithFrame:CGRectMake(3+(aantal*188), 0, 185, 125)]; item.backgroundColor = [UIColor yellowColor]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:@selector(imageTapped:)]; tap.numberOfTapsRequinetworking = 1; tap.cancelsTouchesInView=YES; item.userInteractionEnabled = YES; [item addGestureRecognizer:tap]; [contentOfScrollView addSubview:item]; } //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; //[scrollView addGestureRecognizer:tap]; scrollView.userInteractionEnabled=YES; scrollView.delaysContentTouches=NO; [scrollView addSubview:contentOfScrollView]; [self.view addSubview:scrollView];
Y esta es la function imageTapped.
-(void)imageTapped:(UITapGestureRecognizer *)gesture { NSLog(@"tapped!"); gesture.view.backgroundColor = [UIColor whiteColor]; }
La interacción del usuario se establece en NO
de forma pnetworkingeterminada para UIImageView
, por lo que debe configurarlo en YES
. Lo configuró en sí para "elemento", pero no para contentOfScrollView
.
Tu error está aquí:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:@selector(imageTapped:)];
Debes cambiar el objective a "self" en lugar de "item", entonces no se bloqueará.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];