iOS UIImageView图片添加点击事件

其实很简单,用的就是UITapGestureRecognizer,但是UIImageView的userInteractionEnabled属性默认是NO,对,你没看错,NO!!!

iOS UIImageView图片添加点击事件_第1张图片

所以要把这个参数设置成YES。亲测管用,上代码:

    UIImage *img = [UIImage imageWithContentsOfFile:@"/Users/jinhui005/Desktop/style_img.png"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage)];
    [imgView addGestureRecognizer:tapGesture];
    imgView.userInteractionEnabled = YES;


你可能感兴趣的:(iOS)