本节主要内容:
1.自定义键盘--将工具栏添加到键盘上,并实现切换
2.实现进入到系统相册选取图片
3.实现表情布局
4.实现系统键盘和表情视图之间的切换
5.表情消息的追加
一、*自定义键盘
创建子视图,为系统键盘添加工具栏。例子中创建了textView和一个带有5个button的编辑工具栏
#pragma mark - 创建子视图
- (void)creactSubView{
//1 添加_textView
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-50)];
_textView.backgroundColor = [UIColor clearColor];
_textView.font = [UIFont systemFontOfSize:15.0];
[_textView becomeFirstResponder];
[self.view addSubview:_textView];
//2 编辑栏
_editorView = [[UIView alloc] initWithFrame:CGRectMake(0, _textView.bottom, kScreenWidth, 50)];
_editorView.backgroundColor = [UIColor clearColor];
//***将_editorView添加到系统键盘上,利用下面的inputAccessoryView方法
_textView.inputAccessoryView = _editorView;
//添加5个按钮
NSArray *imgArr = @[
@"compose_toolbar_1.png",
@"compose_toolbar_3.png",
@"compose_toolbar_4.png",
@"compose_toolbar_5.png",
@"compose_toolbar_6.png"
];
CGFloat btnWidth = kScreenWidth/5;
for (int i = 0; i<imgArr.count; i++) {
ThemeButton *button = [[ThemeButton alloc] initWithFrame:CGRectMake(btnWidth*i, 5, btnWidth, 40)];
button.imageName = imgArr[i];
[button addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1000+i;
[_editorView addSubview:button];
}
}
二、*进入系统相册选取图片
按钮1的响应事件—实现进入系统相册选取图片(单张)
switch (button.tag - 1000) {
case 0:
NSLog(@"点击第一个按钮");
{
//由底部弹出的弹窗
UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//选取相册
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate = self;
//模态视图 弹出相册
[self presentViewController:picker animated:YES completion:nil];
}];
[alter addAction:action];
[self presentViewController:alter animated:YES completion:nil];
}
break;
三、*表情布局
要实现的效果:
所有表情分成几页,每页按照4行7列进行排列,点击任意表情会出现该表情的放大镜,放大镜上有该表情和文字。能够实现页面的滑动。
思路:
1.数据处理
从plist文件中取出所有文件,将其存放进一个大的数组中,将这个大数组分割成28个数据一组的小数组,小数组的数量将会对应页面的数量。而在每个小数组里面,这28个数据将会按照4行7列进行排列。
2.页面处理
由外至内视图分别是:UIView->UIScrolleView->UIView->UIImageView(表情视图和选中视图)
代码:
FaceView
.h文件
#import <UIKit/UIKit.h>
#import "UIViewExt.h"
@interface FaceView : UIView