2
This commit is contained in:
@@ -128,6 +128,46 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
|
||||
[self buildRow:self.row4 withRowConfig:rows[3]];
|
||||
}
|
||||
|
||||
#pragma mark - Hit Test
|
||||
|
||||
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
|
||||
UIView *hit = [super hitTest:point withEvent:event];
|
||||
if ([hit isKindOfClass:[KBKeyButton class]]) {
|
||||
return hit;
|
||||
}
|
||||
if (hit == self || hit == self.row1 || hit == self.row2 || hit == self.row3 || hit == self.row4) {
|
||||
KBKeyButton *btn = [self kb_nearestKeyButtonForPoint:point];
|
||||
if (btn) { return btn; }
|
||||
}
|
||||
return hit;
|
||||
}
|
||||
|
||||
- (KBKeyButton *)kb_nearestKeyButtonForPoint:(CGPoint)point {
|
||||
// 扩大按键的可点击区域,优先响应间隙点击(贴近系统键盘手感)。
|
||||
CGFloat slopX = KBFit(8.0f);
|
||||
CGFloat slopY = KBFit(4.0f);
|
||||
KBKeyButton *best = nil;
|
||||
CGFloat bestDistance = CGFLOAT_MAX;
|
||||
NSArray<UIView *> *rows = @[self.row1, self.row2, self.row3, self.row4];
|
||||
for (UIView *row in rows) {
|
||||
for (UIView *view in row.subviews) {
|
||||
if (![view isKindOfClass:[KBKeyButton class]]) { continue; }
|
||||
KBKeyButton *btn = (KBKeyButton *)view;
|
||||
CGRect frame = [self convertRect:btn.frame fromView:row];
|
||||
CGRect hitFrame = CGRectInset(frame, -slopX, -slopY);
|
||||
if (!CGRectContainsPoint(hitFrame, point)) { continue; }
|
||||
CGFloat dx = point.x - CGRectGetMidX(frame);
|
||||
CGFloat dy = point.y - CGRectGetMidY(frame);
|
||||
CGFloat dist = (dx * dx) + (dy * dy);
|
||||
if (dist < bestDistance) {
|
||||
bestDistance = dist;
|
||||
best = btn;
|
||||
}
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
#pragma mark - Key Model Construction
|
||||
|
||||
// 创建当前布局下各行的 KBKey 列表
|
||||
|
||||
Reference in New Issue
Block a user