diff --git a/CustomKeyboard/KeyboardViewController.m b/CustomKeyboard/KeyboardViewController.m index 6156407..3d880ef 100644 --- a/CustomKeyboard/KeyboardViewController.m +++ b/CustomKeyboard/KeyboardViewController.m @@ -729,6 +729,20 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center, - (void)kb_applyTheme { KBSkinTheme *t = [KBSkinManager shared].current; UIImage *img = [[KBSkinManager shared] currentBackgroundImage]; + if ([self kb_isDefaultKeyboardTheme:t]) { + CGSize size = self.bgImageView.bounds.size; + if (size.width <= 0 || size.height <= 0) { + [self.view layoutIfNeeded]; + size = self.bgImageView.bounds.size; + } + if (size.width <= 0 || size.height <= 0) { + size = self.view.bounds.size; + } + if (size.width <= 0 || size.height <= 0) { + size = [UIScreen mainScreen].bounds.size; + } + img = [self kb_defaultGradientImageWithSize:size]; + } NSLog(@"⌨️[Keyboard] apply theme id=%@ hasBg=%d", t.skinId, (img != nil)); [self kb_logSkinDiagnosticsWithTheme:t backgroundImage:img]; self.bgImageView.image = img; @@ -752,6 +766,32 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center, } } +- (BOOL)kb_isDefaultKeyboardTheme:(KBSkinTheme *)theme { + NSString *skinId = theme.skinId ?: @""; + if (skinId.length == 0 || [skinId isEqualToString:@"default"]) { + return YES; + } + return [skinId isEqualToString:kKBDefaultSkinId]; +} + +- (UIImage *)kb_defaultGradientImageWithSize:(CGSize)size { + if (size.width <= 0 || size.height <= 0) { return nil; } + CAGradientLayer *layer = [CAGradientLayer layer]; + layer.frame = CGRectMake(0, 0, size.width, size.height); + layer.startPoint = CGPointMake(0.5, 0.0); + layer.endPoint = CGPointMake(0.5, 1.0); + layer.colors = @[ + (id)[UIColor colorWithHex:0xDEDFE4].CGColor, + (id)[UIColor colorWithHex:0xD1D3DB].CGColor + ]; + + UIGraphicsBeginImageContextWithOptions(size, YES, 0); + [layer renderInContext:UIGraphicsGetCurrentContext()]; + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return image; +} + - (void)kb_logSkinDiagnosticsWithTheme:(KBSkinTheme *)theme backgroundImage:(UIImage *)image { #if DEBUG diff --git a/CustomKeyboard/Resource/normal_them.zip b/CustomKeyboard/Resource/normal_them.zip index b3635b1..9c2b941 100644 Binary files a/CustomKeyboard/Resource/normal_them.zip and b/CustomKeyboard/Resource/normal_them.zip differ diff --git a/CustomKeyboard/View/KBKeyboardView.m b/CustomKeyboard/View/KBKeyboardView.m index 06782f8..61c57fc 100644 --- a/CustomKeyboard/View/KBKeyboardView.m +++ b/CustomKeyboard/View/KBKeyboardView.m @@ -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 *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 列表 diff --git a/keyBoard.xcodeproj/project.pbxproj b/keyBoard.xcodeproj/project.pbxproj index d9d7494..b1263ed 100644 --- a/keyBoard.xcodeproj/project.pbxproj +++ b/keyBoard.xcodeproj/project.pbxproj @@ -172,7 +172,7 @@ 04C6EADD2EAF8CEB0089C901 /* KBToolBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 04C6EADC2EAF8CEB0089C901 /* KBToolBar.m */; }; 04D1F6B22EDFF10A00B12345 /* KBSkinInstallBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 04D1F6B12EDFF10A00B12345 /* KBSkinInstallBridge.m */; }; 04D1F6B32EDFF10A00B12345 /* KBSkinInstallBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 04D1F6B12EDFF10A00B12345 /* KBSkinInstallBridge.m */; }; - 04E1617A2F0FB1320022C23B /* normal_them.zip in Resources */ = {isa = PBXBuildFile; fileRef = 04E161792F0FB1320022C23B /* normal_them.zip */; }; + 04E1617C2F0FCA050022C23B /* normal_them.zip in Resources */ = {isa = PBXBuildFile; fileRef = 04E1617B2F0FCA050022C23B /* normal_them.zip */; }; 04FC95672EB0546C007BD342 /* KBKey.m in Sources */ = {isa = PBXBuildFile; fileRef = 04FC95652EB0546C007BD342 /* KBKey.m */; }; 04FC956A2EB05497007BD342 /* KBKeyButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 04FC95692EB05497007BD342 /* KBKeyButton.m */; }; 04FC956D2EB054B7007BD342 /* KBKeyboardView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04FC956C2EB054B7007BD342 /* KBKeyboardView.m */; }; @@ -540,7 +540,7 @@ 04C6EAE12EAF940F0089C901 /* KBPermissionViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBPermissionViewController.m; sourceTree = ""; }; 04D1F6B02EDFF10A00B12345 /* KBSkinInstallBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBSkinInstallBridge.h; sourceTree = ""; }; 04D1F6B12EDFF10A00B12345 /* KBSkinInstallBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBSkinInstallBridge.m; sourceTree = ""; }; - 04E161792F0FB1320022C23B /* normal_them.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = normal_them.zip; sourceTree = ""; }; + 04E1617B2F0FCA050022C23B /* normal_them.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = normal_them.zip; sourceTree = ""; }; 04FC953A2EAFAE56007BD342 /* KeyBoardPrefixHeader.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeyBoardPrefixHeader.pch; sourceTree = ""; }; 04FC95642EB0546C007BD342 /* KBKey.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBKey.h; sourceTree = ""; }; 04FC95652EB0546C007BD342 /* KBKey.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBKey.m; sourceTree = ""; }; @@ -691,7 +691,7 @@ 041007D02ECE010100D203BB /* Resource */ = { isa = PBXGroup; children = ( - 04E161792F0FB1320022C23B /* normal_them.zip */, + 04E1617B2F0FCA050022C23B /* normal_them.zip */, A1B2C3EC2F20000000000001 /* kb_words.txt */, A1B2C3F02F20000000000002 /* kb_keyboard_layout_config.json */, 0498BDF42EEC50EE006CC1D5 /* emoji_categories.json */, @@ -1826,7 +1826,7 @@ files = ( 04A9FE202EB893F10020DB6D /* Localizable.strings in Resources */, 041007D42ECE012500D203BB /* 002.zip in Resources */, - 04E1617A2F0FB1320022C23B /* normal_them.zip in Resources */, + 04E1617C2F0FCA050022C23B /* normal_them.zip in Resources */, 041007D22ECE012000D203BB /* KBSkinIconMap.strings in Resources */, A1B2C3ED2F20000000000001 /* kb_words.txt in Resources */, A1B2C3F12F20000000000002 /* kb_keyboard_layout_config.json in Resources */,