为UIiamgeview 添加UITapGestureRecognizer手势不起作用的问题

今天在给头像添加点击效果的时候,点击头像的imageview的响应事件不起作用,代码如下:

// 头像

    self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(posX, posY, avatarHW, avatarHW)];

    self.avatarImageView.image = [UIImage imageNamed:@"avatar_default_small"];

    [self.contentView addSubview:self.avatarImageView];

    

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarTap:)];

    tapGesture.delegate = self;

    [self.avatarImageView addGestureRecognizer:tapGesture];


响应事件如下:

- (void)avatarTap:(UITapGestureRecognizer *)tap

{

    if (tap.state == UIGestureRecognizerStateEnded)

    {

        NSLog(@"lllllllllllllllllllllllllll");

    }

}


点击头像以后断点始终不进该响应事件方法中。


经过排查发现,该view已经创建,并且头像没有被遮挡。跟进imgeview的sdk中发现:

@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;               // default is NO

userInteractionEnabled属性默认是NO的,所以应该将头像的userInteractionEnabled属性设为YES,开启人机交互。


于是又查看了一下UIView的userInteractionEnabled属性:

@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // default is YES. if set to NO, user events (touch, keys) are ignored and removed from the event queue.

UIVIew的userInteractionEnabled属性默认是YES


所以,在处理手势的时候我们应该关注一下交互的控件的userInteractionEnabled属性,因为不同的控件的userInteractionEnabled属性默认值会不同。userInteractionEnabled的值为YES的时候手势才会有效果。


转载于:https://my.oschina.net/u/2252309/blog/380326

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值