ios数字键盘,点击手势、滑动、长按手势并存,解决手势冲突问题

嗯,之所以有手势冲突,是因为数字键原来的开发定义的是uibutton,有事件冲突。关掉数字键的响应 enabled = false。手势全部加入到数字键的super view,天然的系统就支持流畅的多手势。如果点击的话,就判断点击的点是否落在UILabel的frame里面即可。

 

    //1-0数字数字背景view
            UIView *numberBtnBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, self.frame.size.width, 50)];
            numberBtnBgView.userInteractionEnabled = YES;
            //增加滑动手势
            [numberBtnBgView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTopViewChangedMouseLocation:)]];
            //增加单击手势
            UITapGestureRecognizer *tapSuperGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(numberTapGesture:)];
            [numberBtnBgView addGestureRecognizer:tapSuperGesture];
            //增加长按手势
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(numberLongPressKeyboardView:)];
            [numberBtnBgView addGestureRecognizer:longPress];
            [self addSubview:numberBtnBgView];
            
            //记录数字键
            self.mutKeyBtns = [NSMutableArray array];
            
            for (int i = 0; i < 10; i ++) {
                YMKeyButton *btn = [[YMKeyButton alloc] initWithFrame:CGRectMake(3+i*(spaceWidth+kYMKeyButtonWidth*(kKeyWindow.width/375.0)), 5, 0, 0)];
                [btn setTitle:@((i+1)%10).stringValue forState:UIControlStateNormal];
                btn.enabled = NO;
                [btn setBackgroundImage:[UIImage imageNamed:@"key_bg_white"] forState:UIControlStateDisabled];
                [self.mutKeyBtns addObject:btn];
                btn.userInteractionEnabled = NO;
                [self addSubview:btn];
            }

对应的手势响应事件:

#pragma mark - 数字键盘左右移动滑动光标手势响应事件
- (void)panTopViewChangedMouseLocation:(UIPanGestureRecognizer *)pan
{
    CGPoint point = [pan translationInView:self];
    static CGPoint center;
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
            center = point;
            break;
        case UIGestureRecognizerStateChanged:
            if (point.x-center.x>5.0) {
                if (_delegate && [_delegate respondsToSelector:@selector(YMKeyboard:moveMouseLocation:)]) {
                    [_delegate YMKeyboard:self moveMouseLocation:1];
                }
                center = point;
            } else if (center.x-point.x>5.0) {
                if (_delegate && [_delegate respondsToSelector:@selector(YMKeyboard:moveMouseLocation:)]) {
                    [_delegate YMKeyboard:self moveMouseLocation:-1];
                }
                center = point;
            }
            break;
            
        default:
            break;
    }
}
#pragma mark - 数字键盘长按手势响应事件
- (void)numberLongPressKeyboardView:(UILongPressGestureRecognizer *)gesture{
    CGPoint point = [gesture locationInView:self];
    NSInteger  clickIndex = -1;
    for (NSInteger i = 0; i<self.mutKeyBtns.count; i++) {
        YMKeyButton *btn = self.mutKeyBtns[i];
        BOOL isContain =  CGRectContainsPoint(btn.frame,point);
        if (isContain) {
            clickIndex = i ;
        }
    }
    if (clickIndex == -1) {
        return;
    }else{
        //如果长按的是数字键盘区域
        YMKeyButton *btn = self.mutKeyBtns[clickIndex];
        switch (gesture.state) {
            case UIGestureRecognizerStateCancelled:
            case UIGestureRecognizerStateEnded:
                [self.popView removeFromSuperview];
                
                // 添加输入框的内容
                if (btn) {}
                
                break;
                
            case UIGestureRecognizerStateBegan:
            case UIGestureRecognizerStateChanged: {
                
                if (!self.popView.superview) {
                    [self.popView showFrom:btn];
                }
                
                break;
            }
            default:
                break;
        }
    }
}
#pragma mark - 1-0 数字点击手势响应事件
- (void)numberTapGesture:(UITapGestureRecognizer *)gesture{
    CGPoint point = [gesture locationInView:self];
    NSInteger  clickIndex = -1;
    for (NSInteger i = 0; i<self.mutKeyBtns.count; i++) {
        YMKeyButton *btn = self.mutKeyBtns[i];
        BOOL isContain =  CGRectContainsPoint(btn.frame,point);
        if (isContain) {
            clickIndex = i ;
        }
    }
    if (clickIndex == -1) {
        return;
    }else{
        [self clickNumberBtn:self.mutKeyBtns[clickIndex]];
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值