From 13facba33a0ec527ed010c4a6e2ffd4c849f1357 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Tue, 28 Oct 2025 20:11:40 +0800 Subject: [PATCH] 4 --- CustomKeyboard/View/KBKeyButton.h | 3 +++ CustomKeyboard/View/KBKeyButton.m | 22 +++++++++++++++++++++- CustomKeyboard/View/KBKeyboardView.m | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CustomKeyboard/View/KBKeyButton.h b/CustomKeyboard/View/KBKeyButton.h index 88ec368..c9a9b8a 100644 --- a/CustomKeyboard/View/KBKeyButton.h +++ b/CustomKeyboard/View/KBKeyButton.h @@ -14,4 +14,7 @@ /// 配置基础样式(背景、圆角等)。创建按钮时调用。 - (void)applyDefaultStyle; +/// 根据选中/高亮等状态刷新外观 +- (void)refreshStateAppearance; + @end diff --git a/CustomKeyboard/View/KBKeyButton.m b/CustomKeyboard/View/KBKeyButton.m index 259c064..68d94f3 100644 --- a/CustomKeyboard/View/KBKeyButton.m +++ b/CustomKeyboard/View/KBKeyButton.m @@ -26,11 +26,31 @@ self.layer.shadowOpacity = 1.0; self.layer.shadowOffset = CGSizeMake(0, 1); self.layer.shadowRadius = 1.5; + [self refreshStateAppearance]; } - (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; - self.alpha = highlighted ? 0.7 : 1.0; // 简单按压反馈 + // 简单按压反馈:选中态不改变透明度,避免和高亮态冲突 + if (self.isSelected) { + self.alpha = 1.0; + } else { + self.alpha = highlighted ? 0.7 : 1.0; + } +} + +- (void)setSelected:(BOOL)selected { + [super setSelected:selected]; + [self refreshStateAppearance]; +} + +- (void)refreshStateAppearance { + // 选中态用于 Shift/CapsLock 等特殊按键的高亮显示 + if (self.isSelected) { + self.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0]; + } else { + self.backgroundColor = [UIColor whiteColor]; + } } @end diff --git a/CustomKeyboard/View/KBKeyboardView.m b/CustomKeyboard/View/KBKeyboardView.m index f822001..b5617c4 100644 --- a/CustomKeyboard/View/KBKeyboardView.m +++ b/CustomKeyboard/View/KBKeyboardView.m @@ -198,6 +198,11 @@ [btn addTarget:self action:@selector(onKeyTapped:) forControlEvents:UIControlEventTouchUpInside]; [row addSubview:btn]; + // Shift 按钮选中态随大小写状态变化 + if (key.type == KBKeyTypeShift) { + btn.selected = self.shiftOn; + } + [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.equalTo(row); if (previous) {