diff --git a/CustomKeyboard/KeyboardViewController.m b/CustomKeyboard/KeyboardViewController.m index e8f08f1..12c1e30 100644 --- a/CustomKeyboard/KeyboardViewController.m +++ b/CustomKeyboard/KeyboardViewController.m @@ -14,7 +14,7 @@ static CGFloat KEYBOARDHEIGHT = 256; -@interface KeyboardViewController () +@interface KeyboardViewController () @property (nonatomic, strong) UIButton *nextKeyboardButton; // 系统“下一个键盘”按钮(可选) @property (nonatomic, strong) KBKeyBoardMainView *keyBoardMainView; // 功能面板视图(点击工具栏第0个时显示) @property (nonatomic, strong) KBFunctionView *functionView; // 功能面板视图(点击工具栏第0个时显示) @@ -22,11 +22,6 @@ static CGFloat KEYBOARDHEIGHT = 256; @implementation KeyboardViewController -- (void)updateViewConstraints { - [super updateViewConstraints]; - - // 可在此添加自定义尺寸约束 -} - (void)viewDidLoad { [super viewDidLoad]; @@ -46,7 +41,6 @@ static CGFloat KEYBOARDHEIGHT = 256; make.bottom.equalTo(self.view.mas_bottom).offset(-4); }]; - self.keyBoardMainView.delegate = self; [self.view addSubview:self.keyBoardMainView]; [self.keyBoardMainView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); @@ -64,7 +58,7 @@ static CGFloat KEYBOARDHEIGHT = 256; // 简单显隐切换,复用相同的布局区域 self.functionView.hidden = !show; self.keyBoardMainView.hidden = show; - + // 可选:把当前显示的视图置顶,避免层级遮挡 if (show) { [self.view bringSubviewToFront:self.functionView]; @@ -76,7 +70,6 @@ static CGFloat KEYBOARDHEIGHT = 256; // MARK: - KBKeyBoardMainViewDelegate - - (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didTapKey:(KBKey *)key { switch (key.type) { case KBKeyTypeCharacter: @@ -99,7 +92,6 @@ static CGFloat KEYBOARDHEIGHT = 256; } - (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didTapToolActionAtIndex:(NSInteger)index { - // 符合你的要求:由控制器来切换面板 if (index == 0) { [self showFunctionPanel:YES]; } else { @@ -107,10 +99,19 @@ static CGFloat KEYBOARDHEIGHT = 256; } } +// MARK: - KBFunctionViewDelegate +- (void)functionView:(KBFunctionView *)functionView didTapToolActionAtIndex:(NSInteger)index { + // 需求:当 index == 0 时,切回键盘主视图 + if (index == 0) { + [self showFunctionPanel:NO]; + } +} + #pragma mark - lazy - (KBKeyBoardMainView *)keyBoardMainView{ if (!_keyBoardMainView) { _keyBoardMainView = [[KBKeyBoardMainView alloc] init]; + _keyBoardMainView.delegate = self; } return _keyBoardMainView; } @@ -118,10 +119,12 @@ static CGFloat KEYBOARDHEIGHT = 256; - (KBFunctionView *)functionView{ if (!_functionView) { _functionView = [[KBFunctionView alloc] init]; + _functionView.delegate = self; // 监听功能面板顶部Bar点击 } return _functionView; } + + + @end - - diff --git a/CustomKeyboard/View/KBFunctionBarView.h b/CustomKeyboard/View/KBFunctionBarView.h index ef6e1a8..e422613 100644 --- a/CustomKeyboard/View/KBFunctionBarView.h +++ b/CustomKeyboard/View/KBFunctionBarView.h @@ -10,8 +10,20 @@ NS_ASSUME_NONNULL_BEGIN /// 功能区顶部的Bar:左侧4个按钮,右侧3个按钮 +@class KBFunctionBarView; + +@protocol KBFunctionBarViewDelegate +@optional +/// 左侧 4 个按钮点击(index: 0~3) +- (void)functionBarView:(KBFunctionBarView *)bar didTapLeftAtIndex:(NSInteger)index; +/// 右侧 3 个按钮点击(index: 0~2) +- (void)functionBarView:(KBFunctionBarView *)bar didTapRightAtIndex:(NSInteger)index; +@end + @interface KBFunctionBarView : UIView +@property (nonatomic, weak, nullable) id delegate; + /// 左侧4个按钮(懒加载创建,等宽水平排布) @property (nonatomic, strong, readonly) NSArray *leftButtons; diff --git a/CustomKeyboard/View/KBFunctionBarView.m b/CustomKeyboard/View/KBFunctionBarView.m index 316fdc1..6b1815b 100644 --- a/CustomKeyboard/View/KBFunctionBarView.m +++ b/CustomKeyboard/View/KBFunctionBarView.m @@ -59,6 +59,7 @@ for (NSInteger i = 0; i < 4; i++) { UIButton *btn = [self buildButtonWithTitle:(i < self.leftTitles.count ? self.leftTitles[i] : [NSString stringWithFormat:@"L%ld", (long)i])]; btn.tag = 100 + i; + [btn addTarget:self action:@selector(onLeftTap:) forControlEvents:UIControlEventTouchUpInside]; [self.leftContainer addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { if (prev) { @@ -84,6 +85,7 @@ UIButton *btn = [self buildButtonWithTitle:(i < self.rightTitles.count ? self.rightTitles[i] : [NSString stringWithFormat:@"R%ld", (long)i])]; btn.tag = 200 + i; [self.rightContainer addSubview:btn]; + [btn addTarget:self action:@selector(onRightTap:) forControlEvents:UIControlEventTouchUpInside]; [rightBtns addObject:btn]; } @@ -112,6 +114,22 @@ self.rightButtonsInternal = rightBtns.copy; } +#pragma mark - Actions + +- (void)onLeftTap:(UIButton *)sender { + NSInteger idx = sender.tag - 100; + if ([self.delegate respondsToSelector:@selector(functionBarView:didTapLeftAtIndex:)]) { + [self.delegate functionBarView:self didTapLeftAtIndex:idx]; + } +} + +- (void)onRightTap:(UIButton *)sender { + NSInteger idx = sender.tag - 200; + if ([self.delegate respondsToSelector:@selector(functionBarView:didTapRightAtIndex:)]) { + [self.delegate functionBarView:self didTapRightAtIndex:idx]; + } +} + - (UIButton *)buildButtonWithTitle:(NSString *)title { UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.layer.cornerRadius = 18; diff --git a/CustomKeyboard/View/KBFunctionView.m b/CustomKeyboard/View/KBFunctionView.m index 8cdcb69..32bcfbe 100644 --- a/CustomKeyboard/View/KBFunctionView.m +++ b/CustomKeyboard/View/KBFunctionView.m @@ -13,7 +13,7 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId"; -@interface KBFunctionView () +@interface KBFunctionView () // UI @property (nonatomic, strong) KBFunctionBarView *barViewInternal; @property (nonatomic, strong) KBFunctionPasteView *pasteViewInternal; @@ -161,10 +161,24 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId"; - (KBFunctionBarView *)barViewInternal { if (!_barViewInternal) { _barViewInternal = [[KBFunctionBarView alloc] init]; + _barViewInternal.delegate = self; // 顶部功能Bar事件下发到本View } return _barViewInternal; } +#pragma mark - KBFunctionBarViewDelegate + +- (void)functionBarView:(KBFunctionBarView *)bar didTapLeftAtIndex:(NSInteger)index { + // 将事件继续透传给上层(如键盘控制器),用于切换界面或其它业务 + if ([self.delegate respondsToSelector:@selector(functionView:didTapToolActionAtIndex:)]) { + [self.delegate functionView:self didTapToolActionAtIndex:index]; + } +} + +- (void)functionBarView:(KBFunctionBarView *)bar didTapRightAtIndex:(NSInteger)index { + // 右侧按钮点击,如收藏/宫格等,按需继续向外抛出(这里暂不定义单独协议方法,可在此内部处理或扩展) +} + - (KBFunctionPasteView *)pasteViewInternal { if (!_pasteViewInternal) { _pasteViewInternal = [[KBFunctionPasteView alloc] init];