嗯,之所以有手势冲突,是因为数字键原来的开发定义的是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]];
}
}