当前位置: 首页>編程日記>正文

键盘表情切换

键盘表情切换

上次把键盘上方的toolbar工具条已经写好,直接拿过来就可以使用。细心的你会发现,每次在使用聊天工具或者微博等社交软件时会发现有一个切换切换到表情的自定义键盘动作。今天就简单的把这个切换键盘的框架搭建好。
首先分析键盘大致分为两部分一部分是表情list(HMEmotionListView),一部分是底部的工具条(HMEmotionToolbar)。
封装自定义键盘(HMEmotionKeyboard包含表情list和工具条)
1–>先自定义HMEmotionKeyboard

#import <UIKit/UIKit.h>
@interface HMEmotionKeyboard : UIView
+(instancetype)keyboard;
@end
#import "HMEmotionKeyboard.h"
#import "UIImage+Extension.h"
#import "UIView+WLFrame.h"
#import "UIView+Extension.h"
#import "HMEmotionListView.h" // 自定义表情
#import "HMEmotionToolbar.h" // 自定义toolbar@interface HMEmotionKeyboard ()<HMEmotionToolbarDelegate>
/** 表情列表 */
@property (nonatomic, weak) HMEmotionListView *listView;
/** 表情工具条 */
@property (nonatomic, weak) HMEmotionToolbar *toollbar;@end@implementation HMEmotionKeyboard+(instancetype)keyboard {return [[self alloc]init];
}- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {// 1.添加表情列表  (设置控件的大小需要在  layoutsubview中写)HMEmotionListView *listView = [[HMEmotionListView alloc] init];listView.backgroundColor = [UIColor greenColor];listView.backgroundColor = [UIColor blackColor];[self addSubview:listView];self.listView = listView;// 2.添加表情工具条HMEmotionToolbar *toollbar = [[HMEmotionToolbar alloc] init];[self addSubview:toollbar];toollbar.delegate = self;self.toollbar = toollbar;}return self;
}#pragma mark - HMEmotionToolbarDelegate
- (void)emotionToolbar:(HMEmotionToolbar *)toolbar didSelectedButton:(HMEmotionType)emotionType
{switch (emotionType) {case HMEmotionTypeDefault:// 默认
//            self.listView.emotions = self.defaultEmotions;break;case HMEmotionTypeEmoji: // Emoji
//            self.listView.emotions = self.emojiEmotions;break;case HMEmotionTypeLxh: // 浪小花
//            self.listView.emotions = self.lxhEmotions;break;default:break;}NSLog(@"%lu %@", (unsigned long)self.listView.emotions.count, [self.listView.emotions firstObject]);
}// 对控件的frame进行设置
-(void)layoutSubviews {[super layoutSubviews];// 设置工具条的frameself.toollbar.x = 0;self.toollbar.width = self.width;self.toollbar.height = 35;self.toollbar.y = self.height - self.toollbar.height;// 设置表情列表的frameself.listView.width = self.width;self.listView.height = self.toollbar.y;}
@end

2–> list表情

#import <UIKit/UIKit.h>
@interface HMEmotionListView : UIView
/** 需要展示的所有表情 */
@property (nonatomic, strong) NSArray *emotions;
@end
#import "HMEmotionListView.h"@implementation HMEmotionListView- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {// Initialization code}return self;
}
@end

3–>底部的toolbar

#import <UIKit/UIKit.h>
@class HMEmotionToolbar;
typedef enum {HMEmotionTypeRecent, // 最近HMEmotionTypeDefault, // 默认HMEmotionTypeEmoji, // EmojiHMEmotionTypeLxh // 浪小花
} HMEmotionType;@protocol HMEmotionToolbarDelegate <NSObject>@optional
- (void)emotionToolbar:(HMEmotionToolbar *)toolbar didSelectedButton:(HMEmotionType)emotionType;
@end@interface HMEmotionToolbar : UIView
@property (nonatomic, weak) id<HMEmotionToolbarDelegate> delegate;
@end
#import "HMEmotionToolbar.h"
#import "UIImage+Extension.h"
#import "UIView+WLFrame.h"
#import "UIView+Extension.h"#define HMEmotionToolbarButtonMaxCount 4@interface HMEmotionToolbar ()
/** 记录当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
@end
@implementation HMEmotionToolbar- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {// 1.添加4个按钮[self setupButton:@"最近" tag:HMEmotionTypeRecent];UIButton *defaultButton = [self setupButton:@"默认" tag:HMEmotionTypeDefault];[self setupButton:@"Emoji" tag:HMEmotionTypeEmoji];[self setupButton:@"浪小花" tag:HMEmotionTypeLxh];// 4.默认选中“默认”按钮[self buttonClick:defaultButton];}return  self;
}/***  添加工具条按钮**  @param title 按钮文字*/
- (UIButton *)setupButton:(NSString *)title tag:(HMEmotionType)tag{UIButton *button = [[UIButton alloc] init];button.tag = tag;// 文字[button setTitle:title forState:UIControlStateNormal];[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];button.titleLabel.font = [UIFont systemFontOfSize:13];// 添加按钮[self addSubview:button];// 设置背景图片int count = self.subviews.count;if (count == 1) { // 第一个按钮//  其中图片需要拉伸[button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_left_normal"] forState:UIControlStateNormal];[button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_left_selected"] forState:UIControlStateSelected];} else if (count == HMEmotionToolbarButtonMaxCount) { // 最后一个按钮[button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_right_normal"] forState:UIControlStateNormal];[button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_right_selected"] forState:UIControlStateSelected];} else { // 中间按钮[button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_mid_normal"] forState:UIControlStateNormal];[button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_mid_selected"] forState:UIControlStateSelected];}return button;
}/***  监听工具条按钮点击 切换键盘*/
- (void)buttonClick:(UIButton *)button
{// 1.控制按钮状态self.selectedButton.selected = NO;button.selected = YES;self.selectedButton = button;// 2.通知代理if ([self.delegate respondsToSelector:@selector(emotionToolbar:didSelectedButton:)]) {[self.delegate emotionToolbar:self didSelectedButton:button.tag];}
}// 对控件的frame进行设置
-(void)layoutSubviews {[super layoutSubviews];// 3.设置工具条按钮的frameCGFloat buttonW = self.width / HMEmotionToolbarButtonMaxCount;CGFloat buttonH = self.height;for (int i = 0; i<HMEmotionToolbarButtonMaxCount; i++) {UIButton *button = self.subviews[i];button.width = buttonW;button.height = buttonH;button.x = i * buttonW;}
}
@end

4–>控制器里设置自定义键盘的属性

@interface ViewController ()< UITextViewDelegate, HMComposeToolbarDelegate>// 遵循键盘工具条代理
@property (nonatomic, strong)HMComposeToolbar *toolbar;

添加键盘工具条

- (void)addToolBar {HMComposeToolbar *toolbar = [[HMComposeToolbar alloc]init];toolbar.width = self.view.width;toolbar.height = 44;toolbar.y = self.view.height - toolbar.height;toolbar.delegate = self;[self.view addSubview:toolbar];self.toolbar = toolbar;
}

实现键盘工具条代理

#pragma mark -- toolBar 代理方法
- (void)composeTool:(HMComposeToolbar *)toolbar didClickedButton:(HMComposeToolbarButtonTypes)buttonType{switch (buttonType) {case HMComposeToolbarButtonTypeEmotion:NSLog(@"表情");[self openEmotion];break;default:break;}
}

初始化自定义键盘

/***  是否正在切换键盘*/
@property (nonatomic, assign, getter = isChangingKeyboard) BOOL changingKeyboard;
@property (nonatomic, strong)HMEmotionKeyboard *keyboard;

键盘出现后只加载一次,用懒加载操作

/***  自定义键盘  懒加载*/
-(HMEmotionKeyboard *)keyboard {if (!_keyboard) {self.keyboard = [HMEmotionKeyboard keyboard];self.keyboard.backgroundColor = [UIColor blueColor];self.keyboard.width = self.view.width;self.keyboard.height = 216;}return _keyboard;
}

点击键盘表情相应方法

#pragma mark -- 表情
- (void)openEmotion {NSLog(@"调用表情");// 正在切换键盘self.changingKeyboard = YES;if (self.textViews.inputView) { // 先判断键盘是否为空,如果是不为空说明是自定义键盘self.textViews.inputView = nil;// 显示表情图片self.toolbar.showEmotionButton = YES;} else { // 如果为空说明是系统自大键盘self.textViews.inputView = self.keyboard;// 不显示表情图片self.toolbar.showEmotionButton = NO;}// 切换键盘前先关闭键盘   然后再打开键盘[self.textViews resignFirstResponder];dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{// 打开键盘[self.textViews becomeFirstResponder];});
}

之前把自定义工具条弹出和拖动退出工具条已经写好。这篇微博只是当你点击键盘表情按钮时候切换系统自带和自定义键盘操作,和自定义键盘下面的工具条。


https://www.fengoutiyan.com/post/14901.html

相关文章:

  • 键盘自带表情怎么设置
  • 键盘切换中文键按哪个
  • 趣键盘输入法表情包
  • 笔记本电脑键盘字母键变成数字键
  • 笔记本小键盘怎么切换
  • 键盘表情符号
  • 平板键盘怎么切换中文
  • 键盘按键错乱怎么恢复
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機