按钮和手势无效问题及解决方案

按钮和手势无效问题
一般有四种情况:
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) {
        
    }];
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android输入法手势操作源码,作者: Himi   输入法手势识别   注意: android.gesture这个类在api-4(SDK1.6)才开始支持的!   提醒:默认存到SD卡中,所以别忘记在AndroidMainfest.xml加上SD卡读写权限!   关于两种方式创建模拟器的SDcard在【Android2D游戏开发之十】有详解    if (Environment.getExternalStorageState() != null) {// 这个方法在试探终端是否有sdcard!    if (!file.exists()) {// 判定是否已经存在手势文件    // 不存在文件的时候我们去直接把我们的手势文件存入    gestureLib.addGesture(name, gesture);    if (gestureLib.save()) {////保存到文件中    gov.clear(true);//清除笔画   // 注意保存的路径默认是/sdcard/gesture ,so~别忘记AndroidMainfest.xml加上读写权限!   // 这里抱怨一下,咳咳、其实昨天就应该出这篇博文的,就是因为这里总是异常,今天仔细看了   // 才发现不是没写权限,而是我虽然在AndroidMainfest.xml中写了权限,但是写错了位置..哭死!   tv.setText("保存手势成功!因为不存在手势文件," + "所以第一次保存手势成功会默认先创" +   "建了一个手势文件!然后将手势保存到文件中.");    et.setText("");    gestureToImage(gesture);    } else {    tv.setText("保存手势失败!");    }    } else {//当存在此文件的时候我们需要先删除此手势然后把新的手势放上    //读取已经存在的文件,得到文件中的所有手势    if (!gestureLib.load()) {//如果读取失败    tv.setText("手势文件读取失败!");    } else {//读取成功   ……内详,请下载代码参阅。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值