【问题标题】:Detect which image was clicked in UIImageView with animationImages使用 animationImages 检测 UIImageView 中单击了哪个图像
【发布时间】:2012-10-12 11:25:49
【问题描述】:

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

现在当用户点击图片时,我捕捉到了点击手势,但问题是我如何知道动画图片中的哪个图片被点击了?

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

【问题讨论】:

  • 如果下面提供的技术不起作用,请添加另一条评论并参考我,因为我有一些其他想法(但更难实现) - 如果提供的技术有效,那就太好了。
  • @DavidH 不幸的是,下面提到的技术不起作用:(

标签: ios


【解决方案1】:

我使用了以下解决方法...我没有使用 animationImages 的内置功能,而是使用自定义 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];
}

【讨论】:

    【解决方案2】:

    你试过 self.image 吗?它应该有当前图像。

    希望大家需要自己设计自定义的animationView来处理。您可以将图像视图添加到视图并定期更新。

    【讨论】:

      【解决方案3】:

      您想要做的事情很困难但并非不可能,并且会使用调试器对您进行一些调查。您需要找到在动画期间在您的每个图像上调用的某种方法。您可能会发现创建一个小型演示项目确实有帮助 - 一个仅包含一些动画图像的单一视图项目。

      我们不知道的是动画运行之后,是所有内容都缓存了,还是在显示之前发送了单个图像。可能会可靠地调用某些方法。

      所以你需要创建一个断点并让它记录一些东西,或者编写一个 UIImage 的子类。您正在寻找的是一种在每个图像上调用的方法。例如,“drawInRect”或“CGImage”。一旦你发现这样的方法在整个动画中被调用,你的问题就解决了。

      然后你将继承 UIImage,并给每个图像一个标签属性和一个委托(使用你编写的新协议),当每个图像获得该绘制调用(或 CGImage 调用)时,它会通知委托它将很快呈现。

      当您点击时,图像将是最后一个注册的图像,它正在提供其 CGImage,或者它已准备好绘制。

      【讨论】:

        【解决方案4】:

        我想您可以使用 [gesture locationInView:self] 捕获手势的位置。如果您还可以捕捉动画的经过时间,您就可以准确计算出点击时屏幕上显示了哪些图像以及点击时落在了哪个可见图像上。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-25
          • 2016-02-08
          • 1970-01-01
          相关资源
          最近更新 更多