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


你可能感兴趣的:(为UIiamgeview 添加UITapGestureRecognizer手势不起作用的问题)