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

本打算实现一个点击按钮 弹出 一个landKindView 然后点击屏幕其他部分时移除这个VIew,没想到的是,出了诸多不可思议的问题。
在给这个控制器的View添加手势时,然后居然拦截不到,touchesbegin方法,然后又试了下添加tapGesture,依旧是没有反应 。
然后我试着 在touchesBegin方法中 实现 [super touchesBegins....];依旧是没有任何反应。无奈,又尝试着在View视图上添加一个landBGView,来承载我想要显示的landKindView。
设置 landBGView 为控制器的View的尺寸,然会添加,到控制器的VIew上。然后在landBGView上添加手势,但是仍旧是没有反应,郁闷至极。。。

今天再次尝试了一下,居然就可以了(可能是自己修改了某些东西)。
<span style="font-family: Arial, Helvetica, sans-serif;">可能的原因:</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 style="white-space:pre">	</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;
            [btn setTitle:landKinds[i]forState:UIControlStateNormal];
            [btn addTarget:selfaction:@selector(landCategoryBtnClick:)forControlEvents:UIControlEventTouchUpInside];
            btn.layer.cornerRadius =5;
            btn.layer.borderWidth =2;
            btn.layer.borderColor = [UIColorlightGrayColor].CGColor;
            
            //[btn setBackgroundColor:[UIColor blueColor]];
            [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];
            
            [alert show];
            break;
        }
        case 1:
            
        case 2:{
            NSString *message =@"BBB,CCC";
            UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"请稍候"message:message delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
            [alert show];
            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];
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Objective-C 编写的手势解锁密码的示例代码: 首先,在你的项目创建一个名为 "GestureLockView" 的 UIView 子类。这个类将用于绘制手势解锁的视图。 GestureLockView.h: ```objective-c #import <UIKit/UIKit.h> @protocol GestureLockViewDelegate <NSObject> - (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path; @end @interface GestureLockView : UIView @property (nonatomic, weak) id<GestureLockViewDelegate> delegate; @end ``` GestureLockView.m: ```objective-c #import "GestureLockView.h" @interface GestureLockView() @property (nonatomic, strong) NSMutableArray *selectedButtons; @end @implementation GestureLockView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.selectedButtons = [NSMutableArray array]; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); for (int i = 0; i < 9; i++) { CGFloat x = (i % 3) * 100 + 25; CGFloat y = (i / 3) * 100 + 25; CGRect frame = CGRectMake(x, y, 50, 50); UIButton *button = [[UIButton alloc] initWithFrame:frame]; button.tag = i + 1; button.layer.cornerRadius = 25.0; button.layer.borderWidth = 1.0; button.layer.borderColor = [UIColor blackColor].CGColor; [button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateSelected]; [button setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } if (self.selectedButtons.count > 0) { UIBezierPath *path = [UIBezierPath bezierPath]; for (int i = 0; i < self.selectedButtons.count; i++) { UIButton *button = self.selectedButtons[i]; CGPoint point = button.center; if (i == 0) { [path moveToPoint:point]; } else { [path addLineToPoint:point]; } } [path addLineToPoint:self.currentPoint]; [[UIColor blueColor] set]; path.lineWidth = 5.0; CGContextAddPath(context, path.CGPath); CGContextStrokePath(context); } } - (void)buttonTapped:(UIButton *)button { button.selected = YES; [self.selectedButtons addObject:button]; [self setNeedsDisplay]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self handleTouches:touches]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self handleTouches:touches]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSMutableString *path = [NSMutableString string]; for (UIButton *button in self.selectedButtons) { [path appendFormat:@"%ld", button.tag]; } [self.delegate gestureLockView:self didFinishWithPath:path]; [self reset]; } - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self reset]; } - (void)handleTouches:(NSSet<UITouch *> *)touches { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; for (UIButton *button in self.subviews) { if (CGRectContainsPoint(button.frame, point)) { if (![self.selectedButtons containsObject:button]) { button.selected = YES; [self.selectedButtons addObject:button]; } break; } } self.currentPoint = point; [self setNeedsDisplay]; } - (void)reset { for (UIButton *button in self.selectedButtons) { button.selected = NO; } [self.selectedButtons removeAllObjects]; [self setNeedsDisplay]; } - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @end ``` 在你的视图控制器,你可以添加一个 GestureLockView 实例,并设置它的 delegate。当手势解锁完成时,GestureLockView 将调用代理方法 gestureLockView:didFinishWithPath:,你可以在这个方法处理解锁的逻辑。 ViewController.h: ```objective-c #import <UIKit/UIKit.h> #import "GestureLockView.h" @interface ViewController : UIViewController <GestureLockViewDelegate> @end ``` ViewController.m: ```objective-c #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) GestureLockView *lockView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.lockView = [[GestureLockView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)]; self.lockView.center = self.view.center; self.lockView.delegate = self; [self.view addSubview:self.lockView]; } - (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path { NSLog(@"Path: %@", path); if ([path isEqualToString:@"123"]) { NSLog(@"Unlock success!"); } else { NSLog(@"Unlock failed!"); } } @end ``` 这里的解锁密码是 "123"。你可以根据需要修改这个密码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值