UIImageView touch detection inside UIScrollView

I have a horizontal scroll filled with UIImageViews .

I want to detect a rollback on a UIImageView and change its background color.

Somehow, the gesture of pressing does not work or so.

However, when I add a scrollview binding gesture, it works. scrollview.background color can be changed.

But I want to discover the answer to the UIImageViews that it contains!

 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.numberOfTapsRequired = 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]; 

And this is the imageTapped function.

 -(void)imageTapped:(UITapGestureRecognizer *)gesture { NSLog(@"tapped!"); gesture.view.backgroundColor = [UIColor whiteColor]; } 
+4
source share
2 answers

By default, NO for the user is set to NO , so you need to set it to YES . You set it for "item", but not for contentOfScrollView .

+12
source

Your mistake:

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:@selector(imageTapped:)]; 

You need to change the target to "I" instead of "item", after which it will not be broken.

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 
+1
source

All Articles