Files
keyboard/CustomKeyboard/View/KBFunctionBarView.m

162 lines
5.6 KiB
Mathematica
Raw Normal View History

2025-10-28 14:30:03 +08:00
//
// KBFunctionBarView.m
// CustomKeyboard
//
// Created by Mac on 2025/10/28.
// - barview
#import "KBFunctionBarView.h"
#import "Masonry.h"
@interface KBFunctionBarView ()
@property (nonatomic, strong) UIView *leftContainer; //
@property (nonatomic, strong) UIView *rightContainer; //
@property (nonatomic, strong) NSArray<UIButton *> *leftButtonsInternal;
@property (nonatomic, strong) NSArray<UIButton *> *rightButtonsInternal;
@end
@implementation KBFunctionBarView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
2025-10-30 13:27:09 +08:00
_leftTitles = @[@"ABC"];
_rightTitles = @[@"Upgrade VIP"];
2025-10-28 14:30:03 +08:00
[self buildUI];
}
return self;
}
#pragma mark - Public
- (NSArray<UIButton *> *)leftButtons { return self.leftButtonsInternal; }
- (NSArray<UIButton *> *)rightButtons { return self.rightButtonsInternal; }
#pragma mark - UI
- (void)buildUI {
// 便
[self addSubview:self.leftContainer];
[self addSubview:self.rightContainer];
[self.rightContainer mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).offset(-12);
make.centerY.equalTo(self.mas_centerY);
make.height.mas_equalTo(36);
}];
[self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(12);
make.right.equalTo(self.rightContainer.mas_left).offset(-12);
make.centerY.equalTo(self.mas_centerY);
make.height.mas_equalTo(36);
}];
// 4
NSMutableArray<UIButton *> *leftBtns = [NSMutableArray arrayWithCapacity:4];
UIView *prev = nil;
2025-10-30 13:27:09 +08:00
for (NSInteger i = 0; i < self.leftTitles.count; i++) {
2025-10-28 14:30:03 +08:00
UIButton *btn = [self buildButtonWithTitle:(i < self.leftTitles.count ? self.leftTitles[i] : [NSString stringWithFormat:@"L%ld", (long)i])];
btn.tag = 100 + i;
2025-10-28 15:18:12 +08:00
[btn addTarget:self action:@selector(onLeftTap:) forControlEvents:UIControlEventTouchUpInside];
2025-10-28 14:30:03 +08:00
[self.leftContainer addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
if (prev) {
make.left.equalTo(prev.mas_right).offset(8);
make.width.equalTo(prev);
} else {
make.left.equalTo(self.leftContainer.mas_left);
}
make.top.bottom.equalTo(self.leftContainer);
}];
prev = btn;
[leftBtns addObject:btn];
}
[prev mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.leftContainer.mas_right);
}];
self.leftButtonsInternal = leftBtns.copy;
2025-10-30 13:57:34 +08:00
// N
2025-10-28 14:30:03 +08:00
NSMutableArray<UIButton *> *rightBtns = [NSMutableArray arrayWithCapacity:3];
2025-10-30 13:27:09 +08:00
for (NSInteger i = 0; i < self.rightTitles.count; i++) {
2025-10-28 14:30:03 +08:00
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];
2025-10-28 15:18:12 +08:00
[btn addTarget:self action:@selector(onRightTap:) forControlEvents:UIControlEventTouchUpInside];
2025-10-28 14:30:03 +08:00
[rightBtns addObject:btn];
}
2025-10-30 13:57:34 +08:00
// 1/2/3...
UIView *prevRight = nil; //
for (NSInteger i = rightBtns.count - 1; i >= 0; i--) {
UIButton *btn = rightBtns[i];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
if (!prevRight) {
//
make.right.equalTo(self.rightContainer.mas_right);
} else {
//
make.right.equalTo(prevRight.mas_left).offset(-8);
make.width.equalTo(prevRight);
}
2025-10-28 14:30:03 +08:00
make.top.bottom.equalTo(self.rightContainer);
}];
2025-10-30 13:57:34 +08:00
prevRight = btn;
}
//
if (prevRight) {
[prevRight mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.greaterThanOrEqualTo(self.rightContainer.mas_left);
2025-10-28 14:30:03 +08:00
}];
}
self.rightButtonsInternal = rightBtns.copy;
}
2025-10-28 15:18:12 +08:00
#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];
}
}
2025-10-28 14:30:03 +08:00
- (UIButton *)buildButtonWithTitle:(NSString *)title {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.layer.cornerRadius = 18;
btn.layer.masksToBounds = YES;
btn.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
btn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
return btn;
}
#pragma mark - Lazy
- (UIView *)leftContainer {
if (!_leftContainer) {
_leftContainer = [[UIView alloc] init];
}
return _leftContainer;
}
- (UIView *)rightContainer {
if (!_rightContainer) {
_rightContainer = [[UIView alloc] init];
}
return _rightContainer;
}
@end