186 lines
6.8 KiB
Objective-C
186 lines
6.8 KiB
Objective-C
//
|
||
// KBFunctionBarView.m
|
||
// CustomKeyboard
|
||
//
|
||
// Created by Mac on 2025/10/28.
|
||
// 功能 - barview
|
||
|
||
#import "KBFunctionBarView.h"
|
||
#import "Masonry.h"
|
||
#import "KBResponderUtils.h" // 查找 UIInputViewController,用于系统切换输入法
|
||
|
||
@interface KBFunctionBarView ()
|
||
@property (nonatomic, strong) UIView *leftContainer; // 左侧按钮容器
|
||
@property (nonatomic, strong) UIView *rightContainer; // 右侧按钮容器
|
||
@property (nonatomic, strong) NSArray<UIButton *> *leftButtonsInternal;
|
||
@property (nonatomic, strong) NSArray<UIButton *> *rightButtonsInternal;
|
||
@property (nonatomic, strong) UIButton *globeButtonInternal; // 可选:系统“切换输入法”键
|
||
@end
|
||
|
||
@implementation KBFunctionBarView
|
||
|
||
- (instancetype)initWithFrame:(CGRect)frame{
|
||
if (self = [super initWithFrame:frame]) {
|
||
self.backgroundColor = [UIColor clearColor];
|
||
// 标题字段暂时不用,预留给后续可能的文案按钮
|
||
_leftTitles = @[];
|
||
_rightTitles = @[];
|
||
[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.globeButtonInternal];
|
||
[self addSubview:self.leftContainer];
|
||
[self addSubview:self.rightContainer];
|
||
|
||
// 右侧:升级 VIP 按钮(使用资源图 upgrad_vip_icon,设计尺寸 115x35)
|
||
[self.rightContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.right.equalTo(self.mas_right).offset(-6);
|
||
make.centerY.equalTo(self.mas_centerY);
|
||
make.width.mas_equalTo(115);
|
||
make.height.mas_equalTo(35);
|
||
}];
|
||
|
||
UIButton *vipButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
vipButton.tag = 200; // 右侧 index = 0
|
||
UIImage *vipImage = [UIImage imageNamed:@"upgrad_vip_icon"];
|
||
[vipButton setImage:vipImage forState:UIControlStateNormal];
|
||
vipButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||
vipButton.adjustsImageWhenHighlighted = YES;
|
||
[vipButton addTarget:self action:@selector(onRightTap:) forControlEvents:UIControlEventTouchUpInside];
|
||
[self.rightContainer addSubview:vipButton];
|
||
[vipButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.edges.equalTo(self.rightContainer);
|
||
}];
|
||
self.rightButtonsInternal = @[vipButton];
|
||
|
||
// 左侧地球键(按需显示,由 kb_refreshGlobeVisibility 控制是否展示)
|
||
[self.globeButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.mas_left).offset(12);
|
||
make.centerY.equalTo(self.mas_centerY);
|
||
make.width.height.mas_equalTo(32);
|
||
}];
|
||
|
||
// 左侧:App 图标按钮(使用资源图 App_icon,图标 34x34,按钮容器 36x36)
|
||
[self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.centerY.equalTo(self.mas_centerY);
|
||
make.width.mas_equalTo(36);
|
||
make.height.mas_equalTo(36);
|
||
make.left.equalTo(self.mas_left).offset(12); // 具体偏移在 kb_refreshGlobeVisibility 中会根据地球键重新设置
|
||
}];
|
||
|
||
UIButton *appButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
appButton.tag = 100; // 左侧 index = 0
|
||
UIImage *appImage = [UIImage imageNamed:@"App_icon"];
|
||
[appButton setImage:appImage forState:UIControlStateNormal];
|
||
appButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||
appButton.adjustsImageWhenHighlighted = YES;
|
||
[appButton addTarget:self action:@selector(onLeftTap:) forControlEvents:UIControlEventTouchUpInside];
|
||
[self.leftContainer addSubview:appButton];
|
||
[appButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.center.equalTo(self.leftContainer);
|
||
make.width.height.mas_equalTo(34); // 设计图尺寸
|
||
}];
|
||
self.leftButtonsInternal = @[appButton];
|
||
|
||
// 初始刷新地球键可见性与事件绑定
|
||
[self kb_refreshGlobeVisibility];
|
||
}
|
||
|
||
#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];
|
||
}
|
||
}
|
||
|
||
#pragma mark - Lazy
|
||
|
||
- (UIView *)leftContainer {
|
||
if (!_leftContainer) {
|
||
_leftContainer = [[UIView alloc] init];
|
||
}
|
||
return _leftContainer;
|
||
}
|
||
|
||
- (UIView *)rightContainer {
|
||
if (!_rightContainer) {
|
||
_rightContainer = [[UIView alloc] init];
|
||
}
|
||
return _rightContainer;
|
||
}
|
||
|
||
- (UIButton *)globeButtonInternal {
|
||
if (!_globeButtonInternal) {
|
||
_globeButtonInternal = [UIButton buttonWithType:UIButtonTypeSystem];
|
||
_globeButtonInternal.layer.cornerRadius = 16;
|
||
_globeButtonInternal.layer.masksToBounds = YES;
|
||
_globeButtonInternal.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
|
||
[_globeButtonInternal setTitle:@"🌐" forState:UIControlStateNormal];
|
||
[_globeButtonInternal setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
||
}
|
||
return _globeButtonInternal;
|
||
}
|
||
|
||
#pragma mark - Globe (Input Mode Switch)
|
||
|
||
- (void)kb_refreshGlobeVisibility {
|
||
UIInputViewController *ivc = KBFindInputViewController(self);
|
||
BOOL needSwitchKey = YES;
|
||
if (ivc && [ivc respondsToSelector:@selector(needsInputModeSwitchKey)]) {
|
||
needSwitchKey = ivc.needsInputModeSwitchKey;
|
||
}
|
||
|
||
self.globeButtonInternal.hidden = !needSwitchKey;
|
||
|
||
// 左容器左约束:根据是否显示地球键动态调整
|
||
[self.leftContainer mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||
if (needSwitchKey) {
|
||
make.left.equalTo(self.globeButtonInternal.mas_right).offset(8);
|
||
} else {
|
||
make.left.equalTo(self.mas_left).offset(12);
|
||
}
|
||
make.centerY.equalTo(self.mas_centerY);
|
||
make.width.mas_equalTo(36);
|
||
make.height.mas_equalTo(36);
|
||
}];
|
||
|
||
// 绑定系统输入法切换事件
|
||
[self.globeButtonInternal removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
|
||
if (needSwitchKey && ivc) {
|
||
SEL sel = NSSelectorFromString(@"handleInputModeListFromView:withEvent:");
|
||
if ([ivc respondsToSelector:sel]) {
|
||
[self.globeButtonInternal addTarget:ivc action:sel forControlEvents:UIControlEventAllTouchEvents];
|
||
} else {
|
||
[self.globeButtonInternal addTarget:ivc action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)didMoveToWindow {
|
||
[super didMoveToWindow];
|
||
[self kb_refreshGlobeVisibility];
|
||
}
|
||
|
||
@end
|