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

Categories

0 votes
876 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...