奇怪的现象:touchesBegan- 与UITapGestureRecognizer手势没有人响应 以及set方法的妙用

发布时间:2025-12-09 13:38:40 浏览次数:4

本打算实现一个点击按钮 弹出 一个landKindView 然后点击屏幕其他部分时移除这个VIew,没想到的是,出了诸多不可思议的问题。在给这个控制器的View添加手势时,然后居然拦截不到,touchesbegin方法,然后又试了下添加tapGesture,依旧是没有反应 。然后我试着 在touchesBegin方法中 实现 [super touchesBegins....];依旧是没有任何反应。无奈,又尝试着在View视图上添加一个landBGView,来承载我想要显示的landKindView。设置 landBGView 为控制器的View的尺寸,然会添加,到控制器的VIew上。然后在landBGView上添加手势,但是仍旧是没有反应,郁闷至极。。。今天再次尝试了一下,居然就可以了(可能是自己修改了某些东西)。
<span >可能的原因:</span>
1. View的userEnable 的属性查看是否开启 (尤其是父控件)
2. View的叠放顺序 查看是否被其他的控件挡住
<img src="https://img-blog.csdn.net/20160115104129525?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="40" alt="" /><img src="https://img-blog.csdn.net/20160115104156814?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="150" alt="" />
3.查看是否添加事件监听
4.查看是否被添加的手势监听拦截
5.对父控件进行上述可能存在的问题检查
(暂时想到这么多)
<span ></span>但是也在尝试做一些其他的东西。重写set方法来实现某些代码的简化,
主要是操作View的显示与隐藏(或者是刷新操作,网络请求之类的均可),主要是为了操作方便,集中处理细节问题
具体如下://某按钮的 控制显示子View- (void)categoryBtnClick{    self.categoryIsShowing = !self.categoryIsShowing ;}// 重写set方法- (void)setCategoryIsShowing:(BOOL)categoryIsShowing{    if (categoryIsShowing == YES) {        _categoryIsShowing =YES;                [self.view addSubview: self.landBGView];    } else {                _categoryIsShowing =NO;        [self.landBGView removeFromSuperview];    }}//这是背景view- (UIView *)landBGView{    if (!_landBGView) {                self.landBGView = [[UIViewalloc] initWithFrame:self.view.bounds];//        self.landBGView.backgroundColor = [UIColor lightGrayColor];                [self.landBGViewaddSubview:self.landKindView];                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(toucheslandBGView)];        [self.landBGView addGestureRecognizer:tapGesture];            }    return_landBGView;}//- (UIView *)landKindView{    if (!_landKindView) {        NSArray *landKinds  = [NSArrayarrayWithObjects:@"AAA",@"BBB",@"CCC",nil];                CGFloat detailViewX = [UIScreenmainScreen].bounds.size.width -100;        //    CGFloat detailViewX = 200;        CGFloat detailViewY = 64;        CGFloat detailViewW = 100;        CGFloat detailViewH = 44 * 3;        UIView  *landKindView = [[UIViewalloc] initWithFrame:CGRectMake(detailViewX, detailViewY, detailViewW, detailViewH) ];        landKindView.backgroundColor = [UIColorlightGrayColor];        CGFloat btnY = 0;        CGFloat btnH = 44;        for (int i =0; i < 3; i++) {            UIButton *btn =  [[UIButtonalloc] initWithFrame:CGRectMake(0, btnY + i*btnH, detailViewW, btnH)];            btn.tag = i;            forState:UIControlStateNormal];            ;            btn.layer.cornerRadius =5;            btn.layer.borderWidth =2;            btn.layer.borderColor = [UIColorlightGrayColor].CGColor;                        //];            [landKindView addSubview:btn];                        UIView *lineView = [[UIViewalloc] initWithFrame:CGRectMake(0 +2, btnY + i*btnH -1, detailViewW - 4, 1)];            lineView.backgroundColor = [UIColorgrayColor];            [landKindView addSubview:lineView];                    }                landKindView.layer.cornerRadius =5;        self.landKindView = landKindView;             }    return_landKindView;}// 分类按钮- (void)landCategoryBtnClick:(UIButton *)btn{    NSInteger index = btn.tag;   switch (index) {        case 0:{            NSString *message = @"AAA";            UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"提示"message:message delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];                        ;            break;        }        case 1:                    case 2:{            NSString *message =@"BBB,CCC";            UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"请稍候"message:message delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];            ;            break;        }        default:            break;    }}#pragma  mark alertView  代理方法- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    [self.landBGViewremoveFromSuperview];    self.categoryIsShowing =NO;        if (buttonIndex == 0) {        return;    }else{        //意见反馈        FeedbackController * feedVC = [[FeedbackControlleralloc]init];        [self.navigationControllershowViewController:feedVC sender:nil];    }    }

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477