使用animationImages检测在UIImageView中单击了哪个图像 [英] Detect which image was clicked in UIImageView with animationImages

查看:94
本文介绍了使用animationImages检测在UIImageView中单击了哪个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以为 UIImageview 指定图像数组,它会很好地为图像设置动画。我已经将 UIImageView 类子类化了。

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
}


推荐答案

我使用了以下解决方法...我没有使用 animationImages 的内置功能,而是使用自定义<$ c为图像设置动画$ c> NSTimer

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

这篇关于使用animationImages检测在UIImageView中单击了哪个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆