1. 程式人生 > >iOS UIImageView圖片新增點選事件

iOS UIImageView圖片新增點選事件

其實很簡單,用的就是UITapGestureRecognizer,但是UIImageView的userInteractionEnabled屬性預設是NO,對,你沒看錯,NO!!!

所以要把這個引數設定成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;