按钮和手势无效问题
一般有四种情况:
1.父类或本身userInteractionEnabled设置了NO。置正确userInteractionEnabled就可以。
2.被上层的组件的事件拦截。如上面有一个文本输出框,虽然你把这个文本输入框设置了userInteractionEnabled为NO。替换不会交互的组件如UILabel。
3.被其它手势事件拦截。如对父视图设置了手势,那么它上面的表格行事件就会无效。参考:《点击UICollectionViewCell和UICollectionView空白处事件响应》。
4.不在感应区内。
本问主要介绍下空间不在感应区的情况。
有人说可以通过view来响应事件来解决,经过实践,确实能不解决该类问题,但是它只能解决父视图和该组件是直接父子关系的情况。我遇到的是正常的父视图和该组件隔了一个有相同问题的view,导致该方法无效。
//返回一个view来响应事件 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ UIView *view = [super hitTest:point withEvent:event]; if (view == nil){ //转换坐标 CGPoint tempPoint = [self.calendarFootView convertPoint:point fromView:self]; //判断点击的点是否在按钮区域内 if (CGRectContainsPoint(self.calendarFootView.bounds, tempPoint)){ //返回按钮 return self.calendarFootView; } } return view; }
下面看我的问题分析和解决方案。不在感应区主要分三种情况:
1.它的父视图的frame设置的过小,该组件在它的视图外面。这个是代码错误,把父视图的frame设置正确就可以。通过View UI Hierarchy直接可以看到父视图和它的frame大小。这个是常见的错误。
2.自己修改了自己的frame,没有在父视图里修改frame。若本组件不想有响应,你自己修改自己的frame没有问题;若需要想有响应事件,需要通过block传递到父视图,让父视图修改frame。
self.calendarView = [[BGCalendarView alloc] initWithIsEnabledSelect:NO icon:[UIImage imageNamed:@"返回 拷贝(1)"] selectedIcon:[UIImage imageNamed:@"返回 拷贝"] selectColor:BGColorHex(FF7648) isHiddenLine:NO lineColor:BGColorHex(F8F8F8) isUnfold:NO frame:CGRectMake(0, kNavBarAndStatusBarHeight, sCommonUnitFullWidth(), (10+15+10+12+5+(sCalendarCellInterval+12+sCalendarCellInterval)+10))]; @weakify(self); self.calendarView.selectBlock = ^(BOOL isSelectContent) { @strongify(self); self.model.isUnfold = isSelectContent; if(isSelectContent) { self.calendarView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, sCommonUnitFullWidth(), (10+15+10+12+5+(sCalendarCellInterval+12+sCalendarCellInterval)*5+10)); self.tabview.frame = CGRectMake(0, kNavBarAndStatusBarHeight+(10+15+10+12+5+(sCalendarCellInterval+12+sCalendarCellInterval)*5+10)-BG_1PX, kUIScreenWidth, FULL_HEIGHT -(kNavBarAndStatusBarHeight+(10+15+10+12+5+(sCalendarCellInterval+12+sCalendarCellInterval)*5+10)+kBottomSafeHeight)+BG_1PX); [self.tabview reloadData]; } else { self.calendarView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, sCommonUnitFullWidth(), (10+15+10+12+5+(sCalendarCellInterval+12+sCalendarCellInterval)+10)); self.tabview.frame = CGRectMake(0, kNavBarAndStatusBarHeight+(10+15+10+12+5+(sCalendarCellInterval+12+sCalendarCellInterval)+10)-BG_1PX, kUIScreenWidth, FULL_HEIGHT -(kNavBarAndStatusBarHeight+(10+15+10+12+5+(sCalendarCellInterval+12+sCalendarCellInterval)+10)+kBottomSafeHeight)+BG_1PX); [self.tabview reloadData]; } };
3.在父视图里修改了组件的位置,但是没有修改组件的frame。 若本组件不想有响应,你父视图里修改了组件的位置没有问题;若需要想有响应事件,需要通过block传递到父视图,让父视图修改frame。
-(void)setupCommonCollectionViewCellView { // self.contentView.backgroundColor = [UIColor whiteColor]; // [self.contentView addSubview:self.bgBtn]; // [self.contentView addSubview:self.decribeImageView]; // [self.decribeImageView addSubview:self.describeTitleLabel]; // [self.decribeImageView addSubview:self.subDescribeTitleLabel]; // [self.decribeImageView addSubview:self.iconImageView]; // [self unitsCommonCollectionViewCellSdLayout]; self.commonSelectView = [[BGCommonSelectView alloc] initWithFrame:CGRectMake(0, 0, sCommonUnitFullWidth(), 43)]; @weakify(self); self.commonSelectView.hitBlock = ^(NSInteger row) { @strongify(self); if(1==row) { [self showShopGoodsDetailParamsSelectView]; } else { [self showShopGoodsDetailSelectView]; } }; [self.contentView addSubview:self.commonSelectView]; } -(void)showShopGoodsDetailParamsSelectView{ if(isCommonUnitEmptyArray(self.model.goods_param)) { return; } self.shopGoodsDetailParamsSelectView = [[CBPShopGoodsDetailParamsSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) bottomViewHeight:(kBottomSafeHeight +self.model.goods_param.count*42+55+45+BG_1PX*2) model:self.model]; [[[PPSingleObject sharedInstance] getLevelNormalWindwow] addSubview:self.shopGoodsDetailParamsSelectView]; [UIView animateWithDuration:0.3 animations:^{ self.shopGoodsDetailParamsSelectView.bottomView.centerY = FULL_HEIGHT - (kBottomSafeHeight+55+45 +self.model.goods_param.count*42)/2; } completion:^(BOOL finished) { }]; } -(void)showShopGoodsDetailSelectView{ if(isCommonUnitEmptyArray(self.model.spec_list)) { return; } self.shopGoodsDetailSelectView = [[CBPShopGoodsDetailSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) bottomViewHeight:(60+75+kBottomSafeHeight +self.model.spec_list.count*(15+12+10+32)+30+30+45+15+BG_1PX+self.model.spec_list.count*BG_1PX) model:self.model]; @weakify(self); self.shopGoodsDetailSelectView.keyboardChangeBlock = ^(CGFloat keyboardHeight) { @strongify(self); self.shopGoodsDetailSelectView.bottomView.frame = CGRectMake(0, FULL_HEIGHT - (keyboardHeight+60+75+kBottomSafeHeight +self.model.spec_list.count*(15+12+10+32)+30+30+45+15+BG_1PX+self.model.spec_list.count*BG_1PX), SCREEN_WIDTH, (60+75+kBottomSafeHeight +self.model.spec_list.count*(15+12+10+32)+30+30+45+15+BG_1PX+self.model.spec_list.count*BG_1PX)); // self.shopGoodsDetailSelectView.bottomView.centerY = FULL_HEIGHT - (60+75+kBottomSafeHeight +self.model.spec_list.count*(15+12+10+32)+30+30+45+15+BG_1PX+self.model.spec_list.count*BG_1PX)/2-keyboardHeight; }; [[[PPSingleObject sharedInstance] getLevelNormalWindwow] addSubview:self.shopGoodsDetailSelectView]; [UIView animateWithDuration:0.3 animations:^{ // self.shopGoodsDetailSelectView.bottomView.centerY = FULL_HEIGHT - (60+75+kBottomSafeHeight +self.model.spec_list.count*(15+12+10+32)+30+30+45+15+BG_1PX+self.model.spec_list.count*BG_1PX)/2; self.shopGoodsDetailSelectView.bottomView.frame = CGRectMake(0, FULL_HEIGHT - (60+75+kBottomSafeHeight +self.model.spec_list.count*(15+12+10+32)+30+30+45+15+BG_1PX+self.model.spec_list.count*BG_1PX), SCREEN_WIDTH, (60+75+kBottomSafeHeight +self.model.spec_list.count*(15+12+10+32)+30+30+45+15+BG_1PX+self.model.spec_list.count*BG_1PX)); } completion:^(BOOL finished) { }]; }