添加功能组件
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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; // 顶部功能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];
|
||||
|
||||
Reference in New Issue
Block a user