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

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

// 头像spa

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

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

    [self.contentView addSubview:self.avatarImageView];get

    

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

    tapGesture.delegate = self;io

    [self.avatarImageView addGestureRecognizer:tapGesture];event


响应事件以下:select

- (void)avatarTap:(UITapGestureRecognizer *)tapsdk

{

    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的时候手势才会有效果。

相关文章
相关标签/搜索