天天看點

按鈕和手勢無效問題及解決方案

按鈕和手勢無效問題

一般有四種情況:

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;
}      
.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];
        }
    };      
-(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) {
        
    }];
}