添加功能组件

This commit is contained in:
2025-10-28 15:18:12 +08:00
parent 2f2f20cfc2
commit a2b51189aa
4 changed files with 60 additions and 13 deletions

View File

@@ -14,7 +14,7 @@
static CGFloat KEYBOARDHEIGHT = 256;
@interface KeyboardViewController () <KBKeyBoardMainViewDelegate>
@interface KeyboardViewController () <KBKeyBoardMainViewDelegate, KBFunctionViewDelegate>
@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

View File

@@ -10,8 +10,20 @@
NS_ASSUME_NONNULL_BEGIN
/// 功能区顶部的Bar左侧4个按钮右侧3个按钮
@class KBFunctionBarView;
@protocol KBFunctionBarViewDelegate <NSObject>
@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<KBFunctionBarViewDelegate> delegate;
/// 左侧4个按钮懒加载创建等宽水平排布
@property (nonatomic, strong, readonly) NSArray<UIButton *> *leftButtons;

View File

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

View File

@@ -13,7 +13,7 @@
static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
@interface KBFunctionView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@interface KBFunctionView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, KBFunctionBarViewDelegate>
// 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; // BarView
}
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];