有的时候需要在图片上添加点击的事件又不想覆盖UIbutton来延时就可以这样了
创建两个 imageView
<p><code>
[imageView1 setUserInteractionEnabled:YES];
[imageView2 setUserInteractionEnabled:YES];
[imageView1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]];
[imageView2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]];
</code></p>
补充
经测试,多个UIImageView不能共用一个UITapGestureRecognizer对象,之前调用的会没效果。根据点击的view判断来自谁的点击
<code>-(void)clickCategory:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(@"click");
NSLog(@"%hhd",[gestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]]);
UIView *viewClicked=[gestureRecognizer view];
if (viewClicked==imageView1) {
NSLog(@"imageView1");
}else if(viewClicked==imageView2)
{
NSLog(@"imageView2");
}
}</code>