This commit is contained in:
2026-01-08 20:04:23 +08:00
parent bbacef4ff7
commit 11d8f78b1b
4 changed files with 84 additions and 4 deletions

View File

@@ -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