大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > IOS技巧 > iOS开发之自定义表情键盘(组件封装与自动布局)

iOS开发之自定义表情键盘(组件封装与自动布局)(3)

关键词:自定义表情键盘表情键盘iOS  阅读(2517) 赞(13)

[摘要]本文是对iOS开发之自定义表情键盘(组件封装与自动布局)的讲解,对学习IOS苹果软件开发有所帮助,与大家分享。

      代码说明:

        1、主要是通过对资源文件或者对从数据库中查询的资源进行遍历然后添加到ScrollView中

        2.为了适应不同的屏幕给相应的组件添加了约束

    3.ToolView组件的封装: ToolView就是在主屏幕上下面的类似于TabBar的东西,当键盘出来的时候,ToolView会运动到键盘上面的位置。为了使用不同的屏幕,也需要用自动布局来实现。

      ToolView.h的代码如下:预留组件接口和声明block类型

 //
 //  ToolView.h
 //  MyKeyBoard
 //
 //  Created by 青玉伏案 on 14-9-16.
 //  Copyright (c) 2014年 Mrli. All rights reserved.
 //
 
 /*****************
  封装下面的工具条组件
  *****************/
 #import <UIKit/UIKit.h>
 
 //定义block块变量类型,用于回调,把本View上的按钮的index传到Controller中
 typedef void (^ToolIndex) (NSInteger index);
 
 @interface ToolView : UIView
 
 //块变量类型的setter方法
 -(void)setToolIndex:(ToolIndex) toolBlock;
 
 @end

      ToolView.m的代码实现:

 //
 //  ToolView.m
 //  MyKeyBoard
 //
 //  Created by 青玉伏案 on 14-9-16.
 //  Copyright (c) 2014年 Mrli. All rights reserved.
 //
 
 #import "ToolView.h"
 
 @interface ToolView ()
 
 //定义ToolIndex类型的block,用于接受外界传过来的block
 @property (nonatomic, strong) ToolIndex myBlock;
 
 @end
 
 
 @implementation ToolView
 
 - (id)initWithFrame:(CGRect)frame
 {
     self = [super initWithFrame:frame];
     if (self) {
        
         //1初始化表情按钮
         UIButton *faceButton = [[UIButton alloc] initWithFrame:CGRectZero];
         faceButton.backgroundColor = [UIColor orangeColor];
         [faceButton setTitle:@"表情" forState:UIControlStateNormal];
         [faceButton setShowsTouchWhenHighlighted:YES];
         [faceButton addTarget:self action:@selector(tapFaceButton:) forControlEvents:UIControlEventTouchUpInside];
         [self addSubview:faceButton];
         
         
         //初始化更多按钮
         UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectZero];
         moreButton.backgroundColor = [UIColor grayColor];
         [moreButton setTitle:@"More" forState:UIControlStateNormal];
         [moreButton setShowsTouchWhenHighlighted:YES];
         [moreButton addTarget:self action:@selector(tapMoreButton:) forControlEvents:UIControlEventTouchUpInside];
         [self addSubview:moreButton];
         
         
         //给我们的按钮添加约束来让按钮来占满toolView;
         faceButton.translatesAutoresizingMaskIntoConstraints = NO;
         moreButton.translatesAutoresizingMaskIntoConstraints = NO;
         
         //添加水平约束
         NSArray *buttonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[faceButton][moreButton(==faceButton)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(faceButton,moreButton)];
         [self addConstraints:buttonH];
         
         //添加垂直约束
         NSArray *button1V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[faceButton]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(faceButton)];
         [self addConstraints:button1V];
         
         NSArray *button2V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[moreButton]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(moreButton)];
         [self addConstraints:button2V];
 
     }
     return self;
 }
 
 //接受传入的回调
 -(void) setToolIndex:(ToolIndex)toolBlock
 {
     self.myBlock = toolBlock;
 }
 
 //点击表情按钮要回调的方法
 -(void) tapFaceButton: (id) sender
 {
     self.myBlock(1);
 }
 
 //点击more要回调的方法
 -(void) tapMoreButton: (id) sender
 {
     self.myBlock(2);
 }
 
 @end

      代码说明:

        主要是对block回调的应用和给相应的组件添加相应的约束

    4.MoreView组件的封装代码就不往上贴啦,和上面的类似,下面是调用MoreView组件的运行效果,有兴趣的读者请自行编写,以上就是视图部分的代码了

     

  二. Mode部分的内容:

    1.先定义我们要使用的数据模型,数据模型如下,time是使用表情的时间,用于排序。

    2.下面编写我们的ImageModelClass类,里面封装了我们操作数据要用的方法

      ImageModelClass.h的代码如下,主要是预留的对外的接口:

 //
 //  ImageModelClass.h
 //  MyKeyBoard
 //
 //  Created by 青玉伏案 on 14-9-16.
 //  Copyright (c) 2014年 Mrli. All rights reserved.
 //
 
 #import <Foundation/Foundation.h>
 #import <CoreData/CoreData.h>
 #import "HistoryImage.h"
 
 @interface ImageModelClass : NSObject
 //保存数据
 -(void)save:(NSData *) image ImageText:(NSString *) imageText;
 //查询所有的图片
 -(NSArray *) queryAll;
 @end

      ImageModelClass.m的代码如下,主要是用CoreData对sqlite的操作:

 //
 //  ImageModelClass.m
 //  MyKeyBoard
 //
 //  Created by 青玉伏案 on 14-9-16.
 //  Copyright (c) 2014年 Mrli. All rights reserved.
 //
 
 #import "ImageModelClass.h"
 
 @interface ImageModelClass ()
 
 @property (nonatomic, strong) NSManagedObjectContext *manager;
 
 @end
 
 @implementation ImageModelClass
 - (instancetype)init
 {
     self = [super init];
     if (self) {
         //通过上下文获取manager
         UIApplication *application = [UIApplication sharedApplication];
         id delegate = application.delegate;
         self.manager = [delegate managedObjectContext];
     }
     return self;
 }
 
 -(void)save:(NSData *)image ImageText:(NSString *)imageText
 {
     if (image != nil) {
         NSArray *result = [self search:imageText];
         
         HistoryImage *myImage;
         
         if (result.count == 0)
         {
             myImage = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([HistoryImage class]) inManagedObjectContext:self.manager];
             myImage.imageText = imageText;
             myImage.headerImage = image;
             myImage.time = [NSDate date];
         }
         else
         {
             myImage = result[0];
             myImage.time = [NSDate date];
         }
         
         //存储实体
         NSError *error = nil;
         if (![self.manager save:&error]) {
             NSLog(@"保存出错%@", [error localizedDescription]);
         }
 
     }
 
 }
 
 
 //查找
 -(NSArray *)search:(NSString *) image
 {
     NSArray *result;
     
         //新建查询条件
         NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([HistoryImage class])];
         
         //添加谓词
         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageText=%@",image];
         
         //把谓词给request
         [fetchRequest setPredicate:predicate];
         
         //执行查询
         NSError *error = nil;
         result = [self.manager executeFetchRequest:fetchRequest error:&error];
         if (error) {
             NSLog(@"查询错误:%@", [error localizedDescription]);
         }
     return result;
 }
 
 
 
 //查询所有的
 -(NSArray *) queryAll
 {
     //新建查询条件
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([HistoryImage class])];
     
     //添加排序规则
     //定义排序规则
     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:NO];
     
     //添加排序规则
     [fetchRequest setSortDescriptors:@[sortDescriptor]];
 
     
     //执行查询
     NSError *error = nil;
     NSArray *result = [self.manager executeFetchRequest:fetchRequest error:&error];
     if (error) {
         NSLog(@"查询错误:%@", [error localizedDescription]);
     }
     
     return result;
 }
 
 @end


相关评论