通过UIImageView的tag为点击事件UITapGestureRecognizer传参

  • Post author:
  • Post category:其他



为了点击图片时,知道哪张图片被点击,需要向

UITapGestureRecognizer

传递一个参数,此处使用了

UIImageView



tag

属性,直接将图片索引写入

imageview



tag

,然后通过

sender.view.tag

去获取图片索引。

UIImageView *imageview = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tab-me-plese.png"]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap:)];

[imageview setTag:i]; //set tag value
[imageview addGestureRecognizer:tap];
[imageview setUserInteractionEnabled:YES];

- (void)processTap:(UIGestureRecognizer *)sender
{
    NSLog(@"点击了第%ld张图片", sender.view.tag);    
}



版权声明:本文为u013993802原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。