Detect which image was clicked in UIImageView with animationImages

We can specify images array for a UIImageview, and it will animate the images very nicely. I have subclassed the UIImageView class.

Now when the user clicks the image, I capture the tap gesture but the problem is how do I know which image in the animationImages was clicked?

  • UIView alpha = 0 causes touches to be dropped to the view below
  • CoreBluetooth: Can't find iPad
  • Use Legacy Swift Language Version - Xcode 8.2
  • How to get the UDID in iOS 6 and iOS 7
  • Recommended IoC framework for iOS?
  • Swift 3 - Comparing Date objects
  • - (void)setup
    {
        self.animationImages = self.bannerImagesArray;
        self.animationDuration = 3.0f;
        self.animationRepeatCount = 0;
        [self startAnimating];
    
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked)];
        [self addGestureRecognizer:tapGesture];
    }
    
    - (void)imageClicked
    {
        NSLog(@"Image clicked");
        // How do i detect which image was clicked here
    }
    

    4 Solutions Collect From Internet About “Detect which image was clicked in UIImageView with animationImages”

    I used the following workaround… Instead of using the built-in feature of animationImages, I animated the images with custom NSTimer

    - (void)setBannerImagesArray:(NSArray *)bannerImagesArray
    {
        _bannerImagesArray = bannerImagesArray;
        [self.timer invalidate];
        self.currentImageIndex = 0;
        self.image = [self.bannerImagesArray objectAtIndex:self.currentImageIndex];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(displayNextImage)
                                                    userInfo:nil
                                                     repeats:YES];
    
    }
    
    - (void)setup
    {
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked)];
        [self addGestureRecognizer:tapGesture];
    
        self.bannerImagesArray = nil;
    }
    
    - (void)imageClicked
    {
        NSLog(@"Image clicked index: %d", self.currentImageIndex);    
    }
    
    - (void)displayNextImage
    {
        self.currentImageIndex = (self.currentImageIndex + 1) % self.bannerImagesArray.count;
        NSLog(@"Current Image Index %d", self.currentImageIndex);
        self.image = [self.bannerImagesArray objectAtIndex:self.currentImageIndex];
    }
    

    Did you try self.image? It should have the current image.

    I hope you need to design your own custom animationView to handle. You may add an imageview to an view and update periodically.

    What you want to do is difficult but not impossible, and will take some investigation on your part using the debugger. What you need to find is some method that is getting called on each one of your images during the animation. You may find creating a small demo project really helps for this – a single view project that just contains a few images that get animated.

    What I we don’t know is after the animation runs, is everything cached or are the individual images messaged prior to display. Likely some method will be called reliably.

    So you will need to either create a breakpoint and have it log something, or write a subclass of UIImage. What you are looking for is a method that gets called on each of the images. for instance, ‘drawInRect’, or ‘CGImage’. Once you find such a method getting called throughout the animation, your problem is solved.

    You will then subclass UIImage, and give each image a tag property, and a delegate (with a new protocol you write), and when each image gets that draw call (or CGImage call), it will inform the delegate that it is going to be rendered shortly.

    When you get a tap, the image will be the last one that registered it was providing its CGImage, or it was ready to draw.

    I suppose you could trap the gesture’s location with [gesture locationInView:self]. If you could also capture the elapsed time of the animation, you could calculate exactly which image(s) were on screen at the time of the tap and on which visible image the tap landed.