今天在給頭像添加點擊效果的時候,點擊頭像的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的時候手勢纔會有效果。