添加emoji1
This commit is contained in:
33
CustomKeyboard/View/KBEmojiPanelView.h
Normal file
33
CustomKeyboard/View/KBEmojiPanelView.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// KBEmojiPanelView.h
|
||||
// CustomKeyboard
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class KBEmojiPanelView, KBSkinTheme;
|
||||
|
||||
@protocol KBEmojiPanelViewDelegate <NSObject>
|
||||
- (void)emojiPanelView:(KBEmojiPanelView *)panel didSelectEmoji:(NSString *)emoji;
|
||||
- (void)emojiPanelViewDidRequestClose:(KBEmojiPanelView *)panel;
|
||||
- (void)emojiPanelViewDidTapSearch:(KBEmojiPanelView *)panel;
|
||||
@end
|
||||
|
||||
@interface KBEmojiPanelView : UIView
|
||||
|
||||
@property (nonatomic, weak) id<KBEmojiPanelViewDelegate> delegate;
|
||||
|
||||
/// 刷新数据(包括常用分类)。
|
||||
- (void)reloadData;
|
||||
|
||||
/// 应用当前主题色
|
||||
- (void)applyTheme:(KBSkinTheme *)theme;
|
||||
|
||||
/// 高亮指定分类
|
||||
- (void)selectCategoryAtIndex:(NSInteger)index;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
459
CustomKeyboard/View/KBEmojiPanelView.m
Normal file
459
CustomKeyboard/View/KBEmojiPanelView.m
Normal file
@@ -0,0 +1,459 @@
|
||||
//
|
||||
// KBEmojiPanelView.m
|
||||
// CustomKeyboard
|
||||
//
|
||||
|
||||
#import "KBEmojiPanelView.h"
|
||||
#import "KBEmojiDataProvider.h"
|
||||
#import "KBSkinManager.h"
|
||||
#import "KBLocalizationManager.h"
|
||||
#import "Masonry.h"
|
||||
|
||||
@interface KBEmojiCollectionCell : UICollectionViewCell
|
||||
@property (nonatomic, strong) UILabel *emojiLabel;
|
||||
- (void)configureWithEmoji:(NSString *)emoji;
|
||||
@end
|
||||
|
||||
@implementation KBEmojiCollectionCell
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame {
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
_emojiLabel = [[UILabel alloc] init];
|
||||
_emojiLabel.font = [UIFont systemFontOfSize:32];
|
||||
_emojiLabel.textAlignment = NSTextAlignmentCenter;
|
||||
_emojiLabel.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
[self.contentView addSubview:_emojiLabel];
|
||||
[NSLayoutConstraint activateConstraints:@[
|
||||
[_emojiLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor],
|
||||
[_emojiLabel.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor],
|
||||
[_emojiLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
|
||||
[_emojiLabel.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],
|
||||
]];
|
||||
self.contentView.layer.cornerRadius = 10;
|
||||
self.contentView.layer.masksToBounds = YES;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)prepareForReuse {
|
||||
[super prepareForReuse];
|
||||
self.emojiLabel.text = @"";
|
||||
}
|
||||
|
||||
- (void)configureWithEmoji:(NSString *)emoji {
|
||||
self.emojiLabel.text = emoji ?: @"";
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface KBEmojiPanelView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||
|
||||
@property (nonatomic, strong) UILabel *titleLabel;
|
||||
@property (nonatomic, strong) UIButton *backButton;
|
||||
@property (nonatomic, strong) UICollectionView *collectionView;
|
||||
@property (nonatomic, strong) UIView *bottomBar;
|
||||
@property (nonatomic, strong) UIScrollView *tabScrollView;
|
||||
@property (nonatomic, strong) UIStackView *tabStackView;
|
||||
@property (nonatomic, strong) UIButton *searchButton;
|
||||
@property (nonatomic, strong) NSArray<UIButton *> *tabButtons;
|
||||
@property (nonatomic, strong) KBEmojiDataProvider *dataProvider;
|
||||
@property (nonatomic, copy) NSArray<KBEmojiCategory *> *categories;
|
||||
@property (nonatomic, assign) NSInteger currentIndex;
|
||||
@property (nonatomic, strong) UIView *magnifierView;
|
||||
@property (nonatomic, strong) UILabel *magnifierLabel;
|
||||
@property (nonatomic, strong) UIColor *tabNormalColor;
|
||||
@property (nonatomic, strong) UIColor *tabSelectedColor;
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBEmojiPanelView
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame {
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
_dataProvider = [KBEmojiDataProvider shared];
|
||||
_currentIndex = 0;
|
||||
[self setupUI];
|
||||
[self registerNotifications];
|
||||
[self reloadData];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
}
|
||||
|
||||
#pragma mark - Setup
|
||||
|
||||
- (void)setupUI {
|
||||
self.backgroundColor = [UIColor colorWithWhite:0.08 alpha:1.0];
|
||||
|
||||
UIView *topBar = [[UIView alloc] init];
|
||||
topBar.backgroundColor = [UIColor clearColor];
|
||||
[self addSubview:topBar];
|
||||
|
||||
self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
self.backButton.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
|
||||
[self.backButton setTitle:@"⌨︎" forState:UIControlStateNormal];
|
||||
[self.backButton addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
|
||||
[topBar addSubview:self.backButton];
|
||||
|
||||
self.titleLabel = [[UILabel alloc] init];
|
||||
self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
|
||||
self.titleLabel.textColor = [UIColor whiteColor];
|
||||
[topBar addSubview:self.titleLabel];
|
||||
|
||||
[topBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.right.equalTo(self);
|
||||
make.top.equalTo(self.mas_top).offset(4);
|
||||
make.height.mas_equalTo(44);
|
||||
}];
|
||||
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(topBar.mas_left).offset(12);
|
||||
make.centerY.equalTo(topBar);
|
||||
make.width.height.mas_equalTo(32);
|
||||
}];
|
||||
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.backButton.mas_right).offset(12);
|
||||
make.centerY.equalTo(topBar);
|
||||
make.right.lessThanOrEqualTo(topBar.mas_right).offset(-12);
|
||||
}];
|
||||
|
||||
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
|
||||
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
|
||||
layout.minimumInteritemSpacing = 8;
|
||||
layout.minimumLineSpacing = 12;
|
||||
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
|
||||
self.collectionView.backgroundColor = [UIColor clearColor];
|
||||
self.collectionView.dataSource = self;
|
||||
self.collectionView.delegate = self;
|
||||
self.collectionView.alwaysBounceVertical = YES;
|
||||
[self.collectionView registerClass:KBEmojiCollectionCell.class forCellWithReuseIdentifier:@"KBEmojiCollectionCell"];
|
||||
[self addSubview:self.collectionView];
|
||||
|
||||
self.bottomBar = [[UIView alloc] init];
|
||||
self.bottomBar.backgroundColor = [UIColor clearColor];
|
||||
[self addSubview:self.bottomBar];
|
||||
|
||||
self.searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
self.searchButton.layer.cornerRadius = 20;
|
||||
self.searchButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
|
||||
[self.searchButton setTitle:KBLocalized(@"Search") forState:UIControlStateNormal];
|
||||
[self.searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
||||
[self.searchButton addTarget:self action:@selector(onSearch) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self.bottomBar addSubview:self.searchButton];
|
||||
|
||||
self.tabScrollView = [[UIScrollView alloc] init];
|
||||
self.tabScrollView.showsHorizontalScrollIndicator = NO;
|
||||
self.tabScrollView.backgroundColor = [UIColor clearColor];
|
||||
[self.bottomBar addSubview:self.tabScrollView];
|
||||
|
||||
self.tabStackView = [[UIStackView alloc] init];
|
||||
self.tabStackView.axis = UILayoutConstraintAxisHorizontal;
|
||||
self.tabStackView.spacing = 8;
|
||||
self.tabStackView.alignment = UIStackViewAlignmentCenter;
|
||||
[self.tabScrollView addSubview:self.tabStackView];
|
||||
|
||||
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.mas_left).offset(12);
|
||||
make.right.equalTo(self.mas_right).offset(-12);
|
||||
make.top.equalTo(topBar.mas_bottom).offset(8);
|
||||
make.bottom.equalTo(self.bottomBar.mas_top).offset(-8);
|
||||
}];
|
||||
|
||||
[self.bottomBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.right.bottom.equalTo(self);
|
||||
make.height.mas_equalTo(72);
|
||||
}];
|
||||
|
||||
[self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.right.equalTo(self.bottomBar.mas_right).offset(-16);
|
||||
make.centerY.equalTo(self.bottomBar);
|
||||
make.width.mas_equalTo(84);
|
||||
make.height.mas_equalTo(40);
|
||||
}];
|
||||
|
||||
[self.tabScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.bottomBar.mas_left).offset(12);
|
||||
make.right.equalTo(self.searchButton.mas_left).offset(-12);
|
||||
make.top.equalTo(self.bottomBar.mas_top).offset(8);
|
||||
make.bottom.equalTo(self.bottomBar.mas_bottom).offset(-8);
|
||||
}];
|
||||
|
||||
[self.tabStackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.edges.equalTo(self.tabScrollView);
|
||||
make.height.equalTo(self.tabScrollView);
|
||||
}];
|
||||
|
||||
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
|
||||
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
|
||||
[self addGestureRecognizer:leftSwipe];
|
||||
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipe:)];
|
||||
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
|
||||
[self addGestureRecognizer:rightSwipe];
|
||||
|
||||
[self applyTheme:[KBSkinManager shared].current];
|
||||
}
|
||||
|
||||
- (void)registerNotifications {
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(onEmojiDataChanged)
|
||||
name:KBEmojiRecentsDidChangeNotification
|
||||
object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(onLocalizationChanged)
|
||||
name:KBLocalizationDidChangeNotification
|
||||
object:nil];
|
||||
}
|
||||
|
||||
#pragma mark - Data
|
||||
|
||||
- (void)reloadData {
|
||||
self.categories = self.dataProvider.categories;
|
||||
if (self.categories.count == 0) {
|
||||
self.currentIndex = NSNotFound;
|
||||
[self.collectionView reloadData];
|
||||
self.titleLabel.text = @"";
|
||||
return;
|
||||
}
|
||||
NSInteger preserved = self.currentIndex;
|
||||
if (preserved < 0 || preserved >= self.categories.count) {
|
||||
preserved = 0;
|
||||
}
|
||||
[self rebuildTabButtons];
|
||||
[self updateSelectionToIndex:preserved];
|
||||
}
|
||||
|
||||
- (void)rebuildTabButtons {
|
||||
for (UIView *v in self.tabStackView.arrangedSubviews) {
|
||||
[self.tabStackView removeArrangedSubview:v];
|
||||
[v removeFromSuperview];
|
||||
}
|
||||
NSMutableArray<UIButton *> *buttons = [NSMutableArray arrayWithCapacity:self.categories.count];
|
||||
[self.categories enumerateObjectsUsingBlock:^(KBEmojiCategory * _Nonnull cat, NSUInteger idx, BOOL * _Nonnull stop) {
|
||||
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
btn.tag = idx;
|
||||
btn.layer.cornerRadius = 16;
|
||||
btn.layer.masksToBounds = YES;
|
||||
btn.titleLabel.font = [UIFont systemFontOfSize:18];
|
||||
[btn setTitle:cat.iconSymbol ?: @"●" forState:UIControlStateNormal];
|
||||
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
||||
[btn addTarget:self action:@selector(onTabTapped:) forControlEvents:UIControlEventTouchUpInside];
|
||||
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 12);
|
||||
[self.tabStackView addArrangedSubview:btn];
|
||||
[buttons addObject:btn];
|
||||
}];
|
||||
self.tabButtons = buttons.copy;
|
||||
}
|
||||
|
||||
- (void)updateSelectionToIndex:(NSInteger)index {
|
||||
if (self.categories.count == 0) {
|
||||
self.currentIndex = NSNotFound;
|
||||
[self.collectionView reloadData];
|
||||
self.titleLabel.text = @"";
|
||||
return;
|
||||
}
|
||||
if (index < 0) { index = 0; }
|
||||
if (index >= self.categories.count) { index = self.categories.count - 1; }
|
||||
self.currentIndex = index;
|
||||
KBEmojiCategory *cat = self.categories[index];
|
||||
self.titleLabel.text = cat.displayTitle;
|
||||
[self.collectionView reloadData];
|
||||
[self updateTabHighlightStates];
|
||||
[self scrollTabToVisible:index];
|
||||
}
|
||||
|
||||
- (void)selectCategoryAtIndex:(NSInteger)index {
|
||||
[self updateSelectionToIndex:index];
|
||||
}
|
||||
|
||||
- (void)updateTabHighlightStates {
|
||||
[self.tabButtons enumerateObjectsUsingBlock:^(UIButton * _Nonnull btn, NSUInteger idx, BOOL * _Nonnull stop) {
|
||||
BOOL selected = (idx == self.currentIndex);
|
||||
btn.backgroundColor = selected ? self.tabSelectedColor : self.tabNormalColor;
|
||||
btn.alpha = selected ? 1.0 : 0.6;
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)scrollTabToVisible:(NSInteger)index {
|
||||
if (index < 0 || index >= self.tabButtons.count) return;
|
||||
UIButton *btn = self.tabButtons[index];
|
||||
CGRect rect = [self.tabScrollView convertRect:btn.frame fromView:self.tabStackView];
|
||||
rect = CGRectInset(rect, -12, 0);
|
||||
[self.tabScrollView scrollRectToVisible:rect animated:YES];
|
||||
}
|
||||
|
||||
#pragma mark - Actions
|
||||
|
||||
- (void)onBack {
|
||||
if ([self.delegate respondsToSelector:@selector(emojiPanelViewDidRequestClose:)]) {
|
||||
[self.delegate emojiPanelViewDidRequestClose:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)onSearch {
|
||||
if ([self.delegate respondsToSelector:@selector(emojiPanelViewDidTapSearch:)]) {
|
||||
[self.delegate emojiPanelViewDidTapSearch:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)onTabTapped:(UIButton *)sender {
|
||||
[self updateSelectionToIndex:sender.tag];
|
||||
}
|
||||
|
||||
- (void)onSwipe:(UISwipeGestureRecognizer *)gesture {
|
||||
if (self.categories.count == 0) return;
|
||||
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
|
||||
if (self.currentIndex + 1 < self.categories.count) {
|
||||
[self updateSelectionToIndex:self.currentIndex + 1];
|
||||
}
|
||||
} else if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
|
||||
if (self.currentIndex - 1 >= 0) {
|
||||
[self updateSelectionToIndex:self.currentIndex - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)onEmojiDataChanged {
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
- (void)onLocalizationChanged {
|
||||
[self.searchButton setTitle:KBLocalized(@"Search") forState:UIControlStateNormal];
|
||||
[self reloadData];
|
||||
}
|
||||
|
||||
#pragma mark - Theme
|
||||
|
||||
- (void)applyTheme:(KBSkinTheme *)theme {
|
||||
UIColor *bg = theme.keyboardBackground ?: [UIColor colorWithWhite:0.08 alpha:1.0];
|
||||
self.backgroundColor = bg;
|
||||
self.collectionView.backgroundColor = [UIColor clearColor];
|
||||
self.titleLabel.textColor = theme.keyTextColor ?: [UIColor whiteColor];
|
||||
UIColor *searchColor = theme.accentColor ?: [UIColor colorWithRed:0.35 green:0.35 blue:0.95 alpha:1];
|
||||
self.searchButton.backgroundColor = searchColor;
|
||||
self.tabNormalColor = [UIColor colorWithWhite:1 alpha:0.08];
|
||||
self.tabSelectedColor = theme.accentColor ?: [UIColor colorWithWhite:1 alpha:0.25];
|
||||
[self updateTabHighlightStates];
|
||||
if (self.magnifierView) {
|
||||
self.magnifierView.backgroundColor = theme.keyBackground ?: [UIColor colorWithWhite:1 alpha:0.9];
|
||||
}
|
||||
if (self.magnifierLabel) {
|
||||
self.magnifierLabel.textColor = theme.keyTextColor ?: [UIColor blackColor];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Magnifier
|
||||
|
||||
- (void)showMagnifierForEmoji:(NSString *)emoji fromRect:(CGRect)rect {
|
||||
if (!self.magnifierView) {
|
||||
self.magnifierView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 68, 68)];
|
||||
self.magnifierView.layer.cornerRadius = 12;
|
||||
self.magnifierView.layer.masksToBounds = YES;
|
||||
self.magnifierView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.3].CGColor;
|
||||
self.magnifierView.layer.shadowOpacity = 0.6;
|
||||
self.magnifierView.layer.shadowOffset = CGSizeMake(0, 2);
|
||||
self.magnifierView.layer.shadowRadius = 3;
|
||||
self.magnifierLabel = [[UILabel alloc] initWithFrame:self.magnifierView.bounds];
|
||||
self.magnifierLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
||||
self.magnifierLabel.textAlignment = NSTextAlignmentCenter;
|
||||
self.magnifierLabel.font = [UIFont systemFontOfSize:40];
|
||||
[self.magnifierView addSubview:self.magnifierLabel];
|
||||
self.magnifierView.alpha = 0;
|
||||
[self addSubview:self.magnifierView];
|
||||
}
|
||||
self.magnifierLabel.text = emoji;
|
||||
CGRect converted = [self convertRect:rect fromView:self.collectionView];
|
||||
CGFloat targetX = CGRectGetMidX(converted);
|
||||
CGFloat targetY = CGRectGetMinY(converted) - CGRectGetHeight(self.magnifierView.bounds)/2 - 8;
|
||||
targetX = MAX(CGRectGetWidth(self.magnifierView.bounds)/2 + 8, targetX);
|
||||
targetX = MIN(CGRectGetWidth(self.bounds) - CGRectGetWidth(self.magnifierView.bounds)/2 - 8, targetX);
|
||||
if (targetY < CGRectGetHeight(self.magnifierView.bounds)/2 + 10) {
|
||||
targetY = CGRectGetHeight(self.magnifierView.bounds)/2 + 10;
|
||||
}
|
||||
self.magnifierView.center = CGPointMake(targetX, targetY);
|
||||
self.magnifierView.hidden = NO;
|
||||
[UIView animateWithDuration:0.08 animations:^{
|
||||
self.magnifierView.alpha = 1.0;
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)hideMagnifier {
|
||||
if (!self.magnifierView) return;
|
||||
[UIView animateWithDuration:0.08 animations:^{
|
||||
self.magnifierView.alpha = 0.0;
|
||||
} completion:^(BOOL finished) {
|
||||
self.magnifierView.hidden = YES;
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - UICollectionViewDataSource
|
||||
|
||||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
||||
if (self.currentIndex == NSNotFound || self.currentIndex >= self.categories.count) {
|
||||
return 0;
|
||||
}
|
||||
KBEmojiCategory *cat = self.categories[self.currentIndex];
|
||||
return cat.items.count;
|
||||
}
|
||||
|
||||
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
KBEmojiCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KBEmojiCollectionCell" forIndexPath:indexPath];
|
||||
KBEmojiCategory *cat = self.categories[self.currentIndex];
|
||||
if (indexPath.item < cat.items.count) {
|
||||
KBEmojiItem *item = cat.items[indexPath.item];
|
||||
[cell configureWithEmoji:item.value];
|
||||
} else {
|
||||
[cell configureWithEmoji:@""];
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
|
||||
#pragma mark - UICollectionViewDelegate
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
if (self.currentIndex == NSNotFound || self.currentIndex >= self.categories.count) return;
|
||||
KBEmojiCategory *cat = self.categories[self.currentIndex];
|
||||
if (indexPath.item >= cat.items.count) return;
|
||||
KBEmojiItem *item = cat.items[indexPath.item];
|
||||
if (item.value.length == 0) return;
|
||||
[self.dataProvider recordEmojiSelection:item.value];
|
||||
if ([self.delegate respondsToSelector:@selector(emojiPanelView:didSelectEmoji:)]) {
|
||||
[self.delegate emojiPanelView:self didSelectEmoji:item.value];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
KBEmojiCategory *cat = (self.currentIndex < self.categories.count) ? self.categories[self.currentIndex] : nil;
|
||||
if (indexPath.item >= cat.items.count) return;
|
||||
KBEmojiItem *item = cat.items[indexPath.item];
|
||||
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
|
||||
if (!cell) return;
|
||||
[self showMagnifierForEmoji:item.value fromRect:cell.frame];
|
||||
}
|
||||
|
||||
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
[self hideMagnifier];
|
||||
}
|
||||
|
||||
#pragma mark - UICollectionViewDelegateFlowLayout
|
||||
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
CGFloat availableWidth = collectionView.bounds.size.width;
|
||||
NSInteger columns = 8;
|
||||
CGFloat spacing = 8;
|
||||
CGFloat totalSpacing = spacing * (columns - 1);
|
||||
CGFloat width = floor((availableWidth - totalSpacing) / columns);
|
||||
if (width < 32) { width = 32; }
|
||||
return CGSizeMake(width, width);
|
||||
}
|
||||
|
||||
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
|
||||
return 12;
|
||||
}
|
||||
|
||||
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
|
||||
return 8;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -23,6 +23,12 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// 点击了右侧设置按钮
|
||||
- (void)keyBoardMainViewDidTapSettings:(KBKeyBoardMainView *)keyBoardMainView;
|
||||
|
||||
/// emoji 视图里选择了一个表情
|
||||
- (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didSelectEmoji:(NSString *)emoji;
|
||||
|
||||
/// emoji 面板点击搜索
|
||||
- (void)keyBoardMainViewDidTapEmojiSearch:(KBKeyBoardMainView *)keyBoardMainView;
|
||||
@end
|
||||
|
||||
@interface KBKeyBoardMainView : UIView
|
||||
|
||||
@@ -10,12 +10,15 @@
|
||||
#import "KBKeyboardView.h"
|
||||
#import "KBFunctionView.h"
|
||||
#import "KBKey.h"
|
||||
#import "KBEmojiPanelView.h"
|
||||
#import "Masonry.h"
|
||||
#import "KBSkinManager.h"
|
||||
|
||||
@interface KBKeyBoardMainView ()<KBToolBarDelegate, KBKeyboardViewDelegate>
|
||||
@interface KBKeyBoardMainView ()<KBToolBarDelegate, KBKeyboardViewDelegate, KBEmojiPanelViewDelegate>
|
||||
@property (nonatomic, strong) KBToolBar *topBar;
|
||||
@property (nonatomic, strong) KBKeyboardView *keyboardView;
|
||||
@property (nonatomic, strong) KBEmojiPanelView *emojiView;
|
||||
@property (nonatomic, assign) BOOL emojiPanelVisible;
|
||||
// 注意:功能面板的展示/隐藏由外部控制器决定,此处不再直接管理显隐
|
||||
@end
|
||||
@implementation KBKeyBoardMainView
|
||||
@@ -23,8 +26,7 @@
|
||||
- (instancetype)initWithFrame:(CGRect)frame {
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
self.backgroundColor = [KBSkinManager shared].current.keyboardBackground;
|
||||
|
||||
|
||||
|
||||
// 顶部栏
|
||||
self.topBar = [[KBToolBar alloc] init];
|
||||
self.topBar.delegate = self;
|
||||
@@ -43,18 +45,60 @@
|
||||
make.bottom.equalTo(self.mas_bottom).offset(-bottomInset);
|
||||
}];
|
||||
|
||||
self.emojiView = [[KBEmojiPanelView alloc] init];
|
||||
self.emojiView.hidden = YES;
|
||||
self.emojiView.alpha = 0.0;
|
||||
self.emojiView.delegate = self;
|
||||
[self addSubview:self.emojiView];
|
||||
[self.emojiView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.edges.equalTo(self);
|
||||
}];
|
||||
|
||||
[self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.right.equalTo(self);
|
||||
make.top.equalTo(self.mas_top).offset(0);
|
||||
make.bottom.equalTo(self.keyboardView.mas_top).offset(-barSpacing);
|
||||
}];
|
||||
|
||||
// 功能面板切换交由外部控制器处理;此处不直接创建/管理
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setEmojiPanelVisible:(BOOL)visible animated:(BOOL)animated {
|
||||
if (self.emojiPanelVisible == visible) return;
|
||||
self.emojiPanelVisible = visible;
|
||||
if (visible) {
|
||||
[self.emojiView reloadData];
|
||||
self.emojiView.hidden = NO;
|
||||
[self bringSubviewToFront:self.emojiView];
|
||||
} else {
|
||||
self.keyboardView.hidden = NO;
|
||||
self.topBar.hidden = NO;
|
||||
}
|
||||
|
||||
void (^changes)(void) = ^{
|
||||
self.emojiView.alpha = visible ? 1.0 : 0.0;
|
||||
self.keyboardView.alpha = visible ? 0.0 : 1.0;
|
||||
self.topBar.alpha = visible ? 0.0 : 1.0;
|
||||
};
|
||||
void (^completion)(BOOL) = ^(BOOL finished) {
|
||||
self.emojiView.hidden = !visible;
|
||||
self.keyboardView.hidden = visible;
|
||||
self.topBar.hidden = visible;
|
||||
};
|
||||
|
||||
if (animated) {
|
||||
[UIView animateWithDuration:0.22 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:changes completion:completion];
|
||||
} else {
|
||||
changes();
|
||||
completion(YES);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)toggleEmojiPanel {
|
||||
[self setEmojiPanelVisible:!self.emojiPanelVisible animated:YES];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - KBToolBarDelegate
|
||||
|
||||
@@ -106,12 +150,15 @@
|
||||
[self.delegate keyBoardMainView:self didTapKey:key];
|
||||
}
|
||||
break;
|
||||
case KBKeyTypeCustom:
|
||||
// 自定义占位:切换语言或其它操作
|
||||
case KBKeyTypeCustom: {
|
||||
if ([key.identifier isEqualToString:KBKeyIdentifierEmojiPanel]) {
|
||||
[self toggleEmojiPanel];
|
||||
break;
|
||||
}
|
||||
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
|
||||
[self.delegate keyBoardMainView:self didTapKey:key];
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
case KBKeyTypeShift:
|
||||
// Shift 已在 KBKeyboardView 内部处理
|
||||
break;
|
||||
@@ -121,6 +168,25 @@
|
||||
// 切换功能面板交由外部控制器处理(此处不再实现)
|
||||
|
||||
// 设置页展示改由 KeyboardViewController 统一处理
|
||||
#pragma mark - KBEmojiPanelViewDelegate
|
||||
|
||||
- (void)emojiPanelView:(KBEmojiPanelView *)panel didSelectEmoji:(NSString *)emoji {
|
||||
if (emoji.length == 0) return;
|
||||
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didSelectEmoji:)]) {
|
||||
[self.delegate keyBoardMainView:self didSelectEmoji:emoji];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)emojiPanelViewDidRequestClose:(KBEmojiPanelView *)panel {
|
||||
[self setEmojiPanelVisible:NO animated:YES];
|
||||
}
|
||||
|
||||
- (void)emojiPanelViewDidTapSearch:(KBEmojiPanelView *)panel {
|
||||
if ([self.delegate respondsToSelector:@selector(keyBoardMainViewDidTapEmojiSearch:)]) {
|
||||
[self.delegate keyBoardMainViewDidTapEmojiSearch:self];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Theme
|
||||
|
||||
- (void)kb_applyTheme {
|
||||
@@ -130,6 +196,9 @@
|
||||
self.backgroundColor = hasImg ? [UIColor clearColor] : bg;
|
||||
self.keyboardView.backgroundColor = hasImg ? [UIColor clearColor] : bg;
|
||||
[self.keyboardView reloadKeys];
|
||||
if (self.emojiView) {
|
||||
[self.emojiView applyTheme:mgr.current];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -279,10 +279,10 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
|
||||
title:@"123"
|
||||
output:@""
|
||||
type:KBKeyTypeModeChange];
|
||||
KBKey *customAI = [KBKey keyWithIdentifier:@"ai"
|
||||
title:@"AI"
|
||||
output:@""
|
||||
type:KBKeyTypeCustom];
|
||||
KBKey *emoji = [KBKey keyWithIdentifier:KBKeyIdentifierEmojiPanel
|
||||
title:@"😊"
|
||||
output:@""
|
||||
type:KBKeyTypeCustom];
|
||||
KBKey *space = [KBKey keyWithIdentifier:@"space"
|
||||
title:@"space"
|
||||
output:@" "
|
||||
@@ -291,7 +291,7 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
|
||||
title:KBLocalized(@"Send")
|
||||
output:@"\n"
|
||||
type:KBKeyTypeReturn];
|
||||
return @[ mode123, customAI, space, ret ];
|
||||
return @[ mode123, emoji, space, ret ];
|
||||
}
|
||||
|
||||
// 底部控制行(数字布局)
|
||||
@@ -300,10 +300,10 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
|
||||
title:@"abc"
|
||||
output:@""
|
||||
type:KBKeyTypeModeChange];
|
||||
KBKey *customAI = [KBKey keyWithIdentifier:@"ai"
|
||||
title:@"AI"
|
||||
output:@""
|
||||
type:KBKeyTypeCustom];
|
||||
KBKey *emoji = [KBKey keyWithIdentifier:KBKeyIdentifierEmojiPanel
|
||||
title:@"😊"
|
||||
output:@""
|
||||
type:KBKeyTypeCustom];
|
||||
KBKey *space = [KBKey keyWithIdentifier:@"space"
|
||||
title:@"space"
|
||||
output:@" "
|
||||
@@ -312,7 +312,7 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
|
||||
title:KBLocalized(@"Send")
|
||||
output:@"\n"
|
||||
type:KBKeyTypeReturn];
|
||||
return @[ modeABC, customAI, space, ret ];
|
||||
return @[ modeABC, emoji, space, ret ];
|
||||
}
|
||||
|
||||
#pragma mark - Row Building
|
||||
|
||||
Reference in New Issue
Block a user