优化键盘弹出宽度

优化键盘按钮视觉效果
This commit is contained in:
2025-11-21 19:40:57 +08:00
parent af8fff5b13
commit fd7b3a7f75
2 changed files with 73 additions and 8 deletions

View File

@@ -10,12 +10,23 @@
@interface KBKeyButton () @interface KBKeyButton ()
// 便 KBKeyboardView // 便 KBKeyboardView
@property (nonatomic, weak, readonly) UIView *kb_keyboardContainer; @property (nonatomic, weak, readonly) UIView *kb_keyboardContainer;
@property (nonatomic, strong) UIImageView *normalImageView; ///
@property (nonatomic, strong) UIColor *baseBackgroundColor; /// / normalImageView
@end @end
@implementation KBKeyButton @implementation KBKeyButton
- (instancetype)initWithFrame:(CGRect)frame { - (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) { if (self = [super initWithFrame:frame]) {
[self addSubview:self.normalImageView];
self.normalImageView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[self.normalImageView.topAnchor constraintEqualToAnchor:self.topAnchor],
[self.normalImageView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[self.normalImageView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:2],
[self.normalImageView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-2],
]];
[self applyDefaultStyle]; [self applyDefaultStyle];
} }
return self; return self;
@@ -26,13 +37,16 @@
KBSkinTheme *t = [KBSkinManager shared].current; KBSkinTheme *t = [KBSkinManager shared].current;
[self setTitleColor:t.keyTextColor forState:UIControlStateNormal]; [self setTitleColor:t.keyTextColor forState:UIControlStateNormal];
[self setTitleColor:t.keyTextColor forState:UIControlStateHighlighted]; [self setTitleColor:t.keyTextColor forState:UIControlStateHighlighted];
self.backgroundColor = t.keyBackground; // normalImageView
self.backgroundColor = [UIColor clearColor];
self.layer.cornerRadius = 6.0; // self.layer.cornerRadius = 6.0; //
self.layer.masksToBounds = NO; self.layer.masksToBounds = NO;
self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.1].CGColor; // self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.1].CGColor; //
self.layer.shadowOpacity = 1.0; self.layer.shadowOpacity = 1.0;
self.layer.shadowOffset = CGSizeMake(0, 1); self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1.5; self.layer.shadowRadius = 1.5;
// 使
[self refreshStateAppearance]; [self refreshStateAppearance];
// //
@@ -67,10 +81,19 @@
// //
// //
CGFloat scale = highlighted ? 0.9 : 1.0; // 0.9~0.95 CGFloat scale = highlighted ? 0.9 : 1.0; // 0.9~0.95
// normalImageView
BOOL hasIcon = (self.iconView.image != nil);
UIColor *normalBgColor = self.baseBackgroundColor ?: [UIColor whiteColor];
UIColor *highlightBgColor = [self kb_darkerColorForColor:normalBgColor];
[UIView animateWithDuration:0.08 [UIView animateWithDuration:0.08
delay:0 delay:0
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut
animations:^{ animations:^{
if (!hasIcon && !self.normalImageView.hidden) {
self.normalImageView.backgroundColor = highlighted ? highlightBgColor : normalBgColor;
}
self.transform = CGAffineTransformMakeScale(scale, scale); self.transform = CGAffineTransformMakeScale(scale, scale);
} }
completion:nil]; completion:nil];
@@ -101,11 +124,26 @@
- (void)refreshStateAppearance { - (void)refreshStateAppearance {
// Shift/CapsLock // Shift/CapsLock
KBSkinTheme *t = [KBSkinManager shared].current; KBSkinTheme *t = [KBSkinManager shared].current;
UIColor *base = nil;
if (self.isSelected) { if (self.isSelected) {
self.backgroundColor = t.keyHighlightBackground ?: t.keyBackground; base = t.keyHighlightBackground ?: t.keyBackground;
} else { } else {
self.backgroundColor = t.keyBackground; base = t.keyBackground;
} }
if (!base) {
base = [UIColor whiteColor];
}
self.baseBackgroundColor = base;
// normalImageView
self.backgroundColor = [UIColor clearColor];
// icon
if (self.iconView.image != nil || self.normalImageView.hidden) {
return;
}
self.normalImageView.backgroundColor = base;
} }
- (void)applyThemeForCurrentKey { - (void)applyThemeForCurrentKey {
@@ -121,16 +159,14 @@
self.iconView.hidden = (iconImg == nil); self.iconView.hidden = (iconImg == nil);
BOOL hasIcon = (iconImg != nil); BOOL hasIcon = (iconImg != nil);
self.normalImageView.hidden = hasIcon;
if (hasIcon) { if (hasIcon) {
// //
[self setTitle:@"" forState:UIControlStateNormal]; [self setTitle:@"" forState:UIControlStateNormal];
[self setTitle:@"" forState:UIControlStateHighlighted]; [self setTitle:@"" forState:UIControlStateHighlighted];
[self setTitle:@"" forState:UIControlStateSelected]; [self setTitle:@"" forState:UIControlStateSelected];
self.titleLabel.hidden = YES; self.titleLabel.hidden = YES;
KBSkinTheme *t = [KBSkinManager shared].current; self.normalImageView.backgroundColor = [UIColor clearColor];
t.keyBackground = [UIColor clearColor];
} else { } else {
// 使 key.title hidden_keys // 使 key.title hidden_keys
[self setTitle:self.key.title forState:UIControlStateNormal]; [self setTitle:self.key.title forState:UIControlStateNormal];
@@ -139,6 +175,34 @@
} }
} }
- (UIImageView *)normalImageView{
if (!_normalImageView) {
_normalImageView = [[UIImageView alloc] init];
// refreshStateAppearance /
_normalImageView.backgroundColor = [UIColor whiteColor];
_normalImageView.layer.cornerRadius = 6;
_normalImageView.layer.masksToBounds = true;
}
return _normalImageView;
}
///
- (UIColor *)kb_darkerColorForColor:(UIColor *)color {
if (!color) return [UIColor colorWithWhite:0.9 alpha:1.0];
CGFloat h = 0, s = 0, b = 0, a = 0;
if ([color getHue:&h saturation:&s brightness:&b alpha:&a]) {
return [UIColor colorWithHue:h saturation:s brightness:MAX(b * 0.9, 0.0) alpha:a];
}
CGFloat white = 0;
if ([color getWhite:&white alpha:&a]) {
return [UIColor colorWithWhite:MAX(white * 0.9, 0.0) alpha:a];
}
return color;
}
@end @end
@implementation KBKeyButton (KBKeyboardContainer) @implementation KBKeyButton (KBKeyboardContainer)

View File

@@ -620,7 +620,8 @@ edgeSpacerMultiplier:(CGFloat)edgeSpacerMultiplier {
// //
CGRect btnFrameInSelf = [button convertRect:button.bounds toView:self]; CGRect btnFrameInSelf = [button convertRect:button.bounds toView:self];
CGFloat previewWidth = MAX(CGRectGetWidth(btnFrameInSelf) * 1.4, 42.0); // CGFloat previewWidth = MAX(CGRectGetWidth(btnFrameInSelf) * 1.4, 42.0);
CGFloat previewWidth = 42;
CGFloat previewHeight = CGRectGetHeight(btnFrameInSelf) * 1.2; CGFloat previewHeight = CGRectGetHeight(btnFrameInSelf) * 1.2;
CGFloat centerX = CGRectGetMidX(btnFrameInSelf); CGFloat centerX = CGRectGetMidX(btnFrameInSelf);
CGFloat centerY = CGRectGetMinY(btnFrameInSelf) - previewHeight * 0.6; CGFloat centerY = CGRectGetMinY(btnFrameInSelf) - previewHeight * 0.6;