iOS 给UIimageView添加UITapGestureRecognizer手势点击事件


在Iphone开发中,像UIimageView是不支持点击的,但往往我们却有很多能在Image上点击的需求

/** 用户头像 */
@property (nonatomic, weak) UIImageView *iconView;
#pragma mark - 延迟加载

/** 用户头像 */

- (UIImageView *)iconView {

    if (!_iconView) {

        UIImageView *iconView = [[UIImageView alloc] init];

        [self.view addSubview:iconView];

        _iconView = iconView;

        iconView.image = [UIImage imageNamed:@"headImage"]; // 默认图片

    }

    return _iconView;

}


- (void)viewDidLoad {
    [super viewDidLoad];

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topViewTap:)];
        self.iconView.userInteractionEnabled = YES;
        [self.iconView addGestureRecognizer:tap];

}


/** 用户点击了顶部UIimageView */
- (void)topViewTap:(UITapGestureRecognizer *)tap {
    NSLog(@"用户点击了图片事件");
    //下面是跳转页面
    AccountVC *infoVC = [[AccountVC alloc] init];
    [self.navigationController pushViewController:infoVC animated:YES];
}

相关