Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
798 views
in Technique[技术] by (71.8m points)

ios - 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?

- (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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...