使用animationImages检测在UIImageView中点击了哪个图像

我们可以为UIImageview指定图像数组,并且它将很好地animation化图像。 我已经分类了UIImageView类。

现在,当用户单击图像时,我捕捉到轻拍手势,但问题是如何知道animationImages中的哪个图像被点击?

 - (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的内置function,我用自定义NSTimeranimation图像

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

你有没有尝试self.image? 它应该有当前的图像。

我希望你需要devise自己的自定义animation视图来处理。 您可以将图像视图添加到视图并定期更新。

你想要做什么是困难的,但不是不可能的,并将采取一些调查你的部分使用debugging器。 你需要find的是在animation过程中每个图像上都会调用的方法。 你可能会发现创build一个小的演示项目真的有助于这个 – 一个单一的视图项目,只包含一些animation的图像。

我们不知道的是在animation运行之后,是否caching了所有内容,或者是在显示之前将单个图像发送给消息。 可能有些方法会被可靠地调用。

所以你需要创build一个断点并logging一些东西,或者写一个UIImage的子类。 你正在寻找的是一个方法,每个图像被调用。 比如“drawInRect”或“CGImage”。 一旦你发现在整个animation中调用这样的方法,你的问题就解决了。

然后,您将inheritanceUIImage的子类,为每个图像分配一个标签属性和一个委托(使用您编写的新协议),并在每个图像获取该绘制调用(或CGImage调用)时,它会通知代表它将要很快被渲染。

当你得到一个水龙头,图像将是最后一个注册它提供其CGImage,或准备绘制。

我想你可以用[手势locationInView:self]来捕捉手势的位置。 如果您还可以捕获animation的运行时间,则可以准确计算出拍摄时屏幕上的哪些图像以及拍摄的可见图像。