iOS组件封装与自动布局自定义表情键盘

2020-01-15 13:48:14王振洲

代码说明:

1.保存图片时先查找图片是否存在,如果存在则更新时间,如果不存在则插入数据(写到这感觉想在用Hibernate写东西)。

三.Controller部分,把上面的组件进行组装

1.MainViewController.m中的延展部分的代码如下:


@interface MainViewController ()
 
//自定义组件
@property (nonatomic, strong) ToolView *toolView;
 
@property (nonatomic, strong) FunctionView *functionView;
 
@property (nonatomic, strong) MoreView *moreView;
 
//系统组件
@property (strong, nonatomic) IBOutlet UITextView *myTextView;
 
@property (strong, nonatomic) NSDictionary *keyBoardDic;
 
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
 
@property (strong, nonatomic) NSString *sendString;
 
//数据model
@property (strong, nonatomic) ImageModelClass *imageMode;
 
@property (strong, nonatomic)HistoryImage *tempImage;
 
@end

2.在viewDidLoad中进行组件的初始化和实现组件的Block回调,代码如下


- (void)viewDidLoad
{
 [super viewDidLoad];
 
 //从sqlite中读取数据
 self.imageMode = [[ImageModelClass alloc] init];
 
 
 //实例化FunctionView
 self.functionView = [[FunctionView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
 self.functionView.backgroundColor = [UIColor blackColor];
 
 //设置资源加载的文件名
 self.functionView.plistFileName = @"emoticons";
 
 __weak __block MainViewController *copy_self = self;
 //获取图片并显示
 [self.functionView setFunctionBlock:^(UIImage *image, NSString *imageText)
 {
 NSString *str = [NSString stringWithFormat:@"%@%@",copy_self.myTextView.text, imageText];
 
 copy_self.myTextView.text = str;
 copy_self.imageView.image = image;
 
 //把使用过的图片存入sqlite
 NSData *imageData = UIImagePNGRepresentation(image);
 [copy_self.imageMode save:imageData ImageText:imageText];
 }];
 
 
 //实例化MoreView
 self.moreView = [[MoreView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
 self.moreView.backgroundColor = [UIColor blackColor];
 [self.moreView setMoreBlock:^(NSInteger index) {
 NSLog(@"MoreIndex = %d",index);
 }];
 
 
 
 //进行ToolView的实例化
 self.toolView = [[ToolView alloc] initWithFrame:CGRectZero];
 self.toolView.backgroundColor = [UIColor blackColor];
 [self.view addSubview:self.toolView];
 
 //给ToolView添加约束
 //开启自动布局
 self.toolView.translatesAutoresizingMaskIntoConstraints = NO;
 
 //水平约束
 NSArray *toolHConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_toolView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_toolView)];
 [self.view addConstraints:toolHConstraint];
 
 //垂直约束
 NSArray *toolVConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[_toolView(44)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_toolView)];
 [self.view addConstraints:toolVConstraint];
 
 
 
 
 //回调toolView中的方法
 [self.toolView setToolIndex:^(NSInteger index)
 {
 NSLog(@"%d", index);
 
 switch (index) {
 case 1:
 [copy_self changeKeyboardToFunction];
 break;
  
 case 2:
 [copy_self changeKeyboardToMore];
 break;
  
 default:
 break;
 }
 
 }];
 
 
 
 //当键盘出来的时候通过通知来获取键盘的信息
 //注册为键盘的监听着
 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 [center addObserver:self selector:@selector(keyNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
 
 
 //给键盘添加dan
 //TextView的键盘定制回收按钮
 UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
 
 UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(tapDone:)];
 UIBarButtonItem * item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 UIBarButtonItem * item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 toolBar.items = @[item2,item1,item3];
 
 self.myTextView.inputAccessoryView =toolBar;
 
}