给ImageView添加触发效果

 UIImageView * headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,10 ,40,40)];
                headImageView.clipsToBounds = YES;
                headImageView.layer.cornerRadius = 20
                //ImageView默认是不可触发的,需要开启
                headImageView.userInteractionEnabled = YES;
                [cell.contentView addSubview:headImageView];
              //给imageView添加点击触发
                UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headViewAction:)];
                [headImageView addGestureRecognizer:tap];
              //因为UIImageView没有tag值,所以放到View上,如果是View的话可直接设tag值
                UIView *singleView = [tap view];
                singleView.tag = 100 +i;
//方法                
- (void)headViewAction:(UIGestureRecognizer *)send
{
    //拿到tag值
    UITapGestureRecognizer * tap = (UITapGestureRecognizer *)send;
    NSInteger i = [tap view].tag;
}

UIView也是和上面基本相同。

你可能感兴趣的:(给ImageView添加触发效果)