简体   繁体   中英

UITapgesturerecognizer on uiimageview inside of uiscrollview

I have an issue where the tap gesture recognizer that I've made only workes on the very last item in the scroll view.

I have an array of images that are getting added to a UIImageView and the UIImageView is getting add to the ScrollView in the following code:

UIImageView *imageView1;
for (int i = 0; i < numberOfViews; i++) {
    xOrigin = i * imageSize;
    imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,50,100,50)];
    [imageView1 setUserInteractionEnabled:YES];
    [imageView1 addGestureRecognizer:tap]; // The gesture I want
    [imageView1 setImage:[images objectAtIndex:i]];
    [scrollView addSubview:imageView1];
}
// Set the contentSize equal to the size of the UIImageView
// scrollView.contentSize = imageView.scrollview.size;
scrollView.contentSize = CGSizeMake(numberOfViews * imageSize, imageSize);


// Finally, add the UIScrollView to the controller's view
[self.view addSubview:scrollView];

The above code works properly for adding the images in order onto the scroll view as well as adding the tap to the very last item in the on the list. That is, the rest of them are not getting the tap action?

Thanks in advanced.

as i can see you have only one tap gesture object , that why when you run the loop, it get added to last image view only.

you need to create one tap gesture object for one image view.

UIImageView *imageView1;
for (int i = 0; i < numberOfViews; i++) {
    xOrigin = i * imageSize;
    imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,50,100,50)];
    [imageView1 setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapMethod:)];
    [imageView1 addGestureRecognizer:tap]; // The gesture I want
    [imageView1 setImage:[images objectAtIndex:i]];
    [scrollView addSubview:imageView1];
}
// Set the contentSize equal to the size of the UIImageView
// scrollView.contentSize = imageView.scrollview.size;
scrollView.contentSize = CGSizeMake(numberOfViews * imageSize, imageSize);
// Finally, add the UIScrollView to the controller's view
[self.view addSubview:scrollView];

Well, this makes sense, since you add one gesture recognizer to all your image views. When you add the same gesture recognizer to the second view it is removed from the previous view. You should either add one gesture recognizer to your scrollview and then determine which image view was tapped based on the touch location, or add separate gesture recognizer to each of your image views.

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
UIGestureRecognizer *tapGestureRecongnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[scrollView addGestureRecognizer:tapGestureRecongnizer];


- (void)handleTap:(UIGestureRecognizer *)tapGestureRecognizer
{
    CGPoint location = [tapGestureRecognizer locationInView:self.scrollView];
    for (UIImageView *imageView in self.imageViews) {
        if (CGRectContainsPoint(imageView.frame, location)) {
            // here is the imageView being tapped
        }
    }
}

Another way would be to use UIButton s instead of UIImageView s, then you would also get a "highlighted effect" for free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM