上面的器械是編寫自界說的臉色鍵盤,話不多說,直言不諱吧!上面重要用到的常識有MVC, IOS開辟中的主動結構,自界說組件的封裝與應用,Block回調,CoreData的應用。有的小同伴能夠會問寫一個自界說臉色鍵盤腫麼這麼費事?上面 將會引見我們若何用下面提到的器械來界說我們的臉色鍵盤的。上面的內容會比擬多,這篇文章照樣比擬有料的。
照樣那句話寫技巧博客是少不了代碼的,上面會聯合代碼往返顧一下IOS的常識,本篇博文頂用到的常識點在後面的博客中都能找到響應的內容,本篇 算是一個小小的功效整合。先來張圖看一下本app的目次構造。我是依據本身對MVC的懂得來構建的目次構造,願望起到拋磚引玉的感化,有好的處理計劃迎接 評論或許留言指出。Face文件中寄存的時我們的臉色圖片,Model文件封裝的是從SQLite中讀取汗青頭像的組件,View文件中封裝的時我們自界說的組件,也就是自界說鍵盤相干的視圖,Controller擔任將我們的各個組件組裝到一路完成我們想要的功效。上面會逐個引見。

下面是文件的組織構造,上面為了更加直不雅的懂得我們想要的後果,上面先看幾張截圖,來直不雅的感觸感染一下運轉後果,下面是豎屏的顯示後果,上面是橫 屏的顯示後果。由於在封裝自界說鍵盤頂用到了主動結構所以橫屏顯示或許在更年夜的屏幕上顯示是沒成績的,經常使用臉色是用戶用過的臉色,然後存在SQLite 中,顯示時並按時光降序分列。more是用來擴大功效用的接口。話不多說,來的代碼才是其實的。

一.View(自界說視圖)
View文件夾下寄存的時我們自界說的視圖組件,由於是自界說的組件所以storyboard我們就用不了啦,一切的代碼都必需手寫,如許 能力包管組件應用的靈巧性和削減各個組件之間的耦合性,更利於團隊之間的協作。在封裝組件時要預留好外界能夠應用到的接口,和前往該前往的數據。好啦,空話少說,來點干貨吧!
1、FaceView組件的封裝:FaceView即擔任顯示一個個的頭像。在應用該組件時要傳入要顯示的圖片和圖片對應的文字(如【哈 哈】),當點擊圖片的時刻,會經由過程block回調的情勢把該圖片的image和圖片文字前往到應用的組件中去,上面是症結代碼:
FaceView.h中的代碼以下(上面代碼是界說啦響應的Block類型和對外的接口):
#import //聲明臉色對應的block,用於把點擊的臉色的圖片和圖片信息傳到下層視圖 typedef void (^FaceBlock) (UIImage *image, NSString *imageText); @interface FaceView : UIView //圖片對應的文字 @property (nonatomic, strong) NSString *imageText; //臉色圖片 @property (nonatomic, strong) UIImage *headerImage; //設置block回調 -(void)setFaceBlock:(FaceBlock)block; //設置圖片,文字 -(void)setImage:(UIImage *) image ImageText:(NSString *) text; @end
FaceView.m中的代碼以下
// FaceView.m
// MyKeyBoard
//
// Created by 青玉伏案 on 14-9-16.
// Copyright (c) 2014年 Mrli. All rights reserved.
//
#import "FaceView.h"
@interface FaceView ()
@property(strong, nonatomic) FaceBlock block;
@property (strong, nonatomic) UIImageView *imageView;
@end
@implementation FaceView
//初始化圖片
- (id)initWithFrame:(CGRect)frame
{
//face的年夜小
frame.size.height = 30;
frame.size.width = 30;
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[self addSubview:self.imageView];
}
return self;
}
-(void) setFaceBlock:(FaceBlock)block
{
self.block = block;
}
-(void) setImage:(UIImage *)image ImageText:(NSString *)text
{
//顯示圖片
[self.imageView setImage:image];
//把圖片存儲起來
self.headerImage = image;
self.imageText = text;
}
//點擊時回調
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//斷定觸摸的停止點能否在圖片中
if (CGRectContainsPoint(self.bounds, point))
{
//回調,把該頭像的信息傳到響應的controller中
self.block(self.headerImage, self.imageText);
}
}
@end
代碼解釋:
重要就是block回調的應用,就是封裝了一個自界說的button
2、FunctionView組件的封裝,FunctionView就是應用FaceView組件和ScrollView組件把臉色加載出去,在實例化FunctionView組件時,我們用到了主動結構來設置ScrollView和上面的Button.
FunctionView.h的代碼以下,在.h中留有組件的接口和回挪用的Block, plistFileName用於加載我們的資本文件時應用。
// FunctionView.h // MyKeyBoard // // Created by 青玉伏案 on 14-9-16. // Copyright (c) 2014年 Mrli. All rights reserved. // #import //界說對應的block類型,用於數據的交互 typedef void (^FunctionBlock) (UIImage *image, NSString *imageText); @interface FunctionView : UIView //資本文件名 @property (nonatomic, strong) NSString *plistFileName; //接收block塊 -(void)setFunctionBlock:(FunctionBlock) block; @end
FunctionView.m中的代碼以下,經常使用臉色是在SQLite中獲得的,而全體臉色是經由過程plist文件的信息在Face文件中加載的:
// FunctionView.m
// MyKeyBoard
//
// Created by 青玉伏案 on 14-9-16.
// Copyright (c) 2014年 Mrli. All rights reserved.
//
#import "FunctionView.h"
#import "FaceView.h"
#import "ImageModelClass.h"
#import "HistoryImage.h"
@interface FunctionView()
@property (strong, nonatomic) FunctionBlock block;
//暫存臉色組件回調的臉色和臉色文字
@property (strong, nonatomic) UIImage *headerImage;
@property (strong, nonatomic) NSString *imageText;
//display我們的臉色圖片
@property (strong, nonatomic) UIScrollView *headerScrollView;
//界說數據模子用於獲得汗青臉色
@property (strong, nonatomic) ImageModelClass *imageModel;
@end
@implementation FunctionView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//實例化數據模子
self.imageModel =[[ImageModelClass alloc] init];
//實例化上面的button
UIButton *faceButton = [[UIButton alloc] initWithFrame:CGRectZero];
faceButton.backgroundColor = [UIColor grayColor];
[faceButton setTitle:@"全體臉色" forState:UIControlStateNormal];
[faceButton setShowsTouchWhenHighlighted:YES];
[faceButton addTarget:self action:@selector(tapButton1:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:faceButton];
//實例化經常使用臉色按鈕
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectZero];
moreButton.backgroundColor = [UIColor orangeColor];
[moreButton setTitle:@"經常使用臉色" forState:UIControlStateNormal];
[moreButton setShowsTouchWhenHighlighted:YES];
[moreButton addTarget:self action:@selector(tapButton2:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:moreButton];
//給按鈕添加束縛
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(44)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(faceButton)];
[self addConstraints:button1V];
NSArray *button2V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[moreButton(44)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(moreButton)];
[self addConstraints:button2V];
//默許顯示臉色圖片
[self tapButton1:nil];
}
return self;
}
//接收回調
-(void)setFunctionBlock:(FunctionBlock)block
{
self.block = block;
}
//點擊全體臉色按鈕回調辦法
-(void)tapButton1: (id) sender
{
// 從plist文件載入資本
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:self.plistFileName ofType:@"plist"];
NSArray *headers = [NSArray arrayWithContentsOfFile:path];
if (headers.count == 0) {
NSLog(@"拜訪的plist文件不存在");
}
else
{
//挪用headers辦法顯示臉色
[self header:headers];
}
}
//點擊汗青臉色的回調辦法
-(void) tapButton2: (id) sender
{
//從數據庫中查詢一切的圖片
NSArray *imageData = [self.imageModel queryAll];
//解析要求到的數據
NSMutableArray *headers = [NSMutableArray arrayWithCapacity:imageData.count];
//數據實體,相當於javaBean的器械
HistoryImage *tempData;
for (int i = 0; i < imageData.count; i ++) {
tempData = imageData[i];
//解析數據,轉換成函數headers要用的數據格局
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2];
[dic setObject:tempData.imageText forKey:@"chs"];
UIImage *image = [UIImage imageWithData:tempData.headerImage];
[dic setObject:image forKey:@"png"];
[headers addObject:dic];
}
[self header:headers];
}
//擔任把查出來的圖片顯示
-(void) header:(NSArray *)headers
{
[self.headerScrollView removeFromSuperview];
self.headerScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.headerScrollView];
//給scrollView添加束縛
self.headerScrollView.translatesAutoresizingMaskIntoConstraints = NO;
//程度束縛
NSArray *scrollH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_headerScrollView]-10-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_headerScrollView)];
[self addConstraints:scrollH];
//垂直束縛
NSArray *scrolV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_headerScrollView]-50-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_headerScrollView)];
[self addConstraints:scrolV];
CGFloat scrollHeight = (self.frame).size.height-60;
//依據圖片量來盤算scrollView的Contain的寬度
CGFloat width = (headers.count/(scrollHeight/30))*30;
self.headerScrollView.contentSize = CGSizeMake(width, scrollHeight);
self.headerScrollView.pagingEnabled = YES;
//圖片坐標
CGFloat x = 0;
CGFloat y = 0;
//往scroll上貼圖片
for (int i = 0; i < headers.count; i ++) {
//獲得圖片信息
UIImage *image;
if ([headers[i][@"png"] isKindOfClass:[NSString class]])
{
image = [UIImage imageNamed:headers[i][@"png"]];
}
else
{
image = headers[i][@"png"];
}
NSString *imageText = headers[i][@"chs"];
//盤算圖片地位
y = (i%(int)(scrollHeight/30)) * 30;
x = (i/(int)(scrollHeight/30)) * 30;
FaceView *face = [[FaceView alloc] initWithFrame:CGRectMake(x, y, 0, 0)];
[face setImage:image ImageText:imageText];
//face的回調,當face點擊時獲得face的圖片
__weak __block FunctionView *copy_self = self;
[face setFaceBlock:^(UIImage *image, NSString *imageText)
{
copy_self.block(image, imageText);
}];
[self.headerScrollView addSubview:face];
}
[self.headerScrollView setNeedsDisplay];
}
@end
代碼解釋:
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 //界說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 #import #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
代碼解釋:
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;
}
3.當反正屏幕切換時設置自界說鍵盤的高度
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//縱屏
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
CGRect frame = self.functionView.frame;
frame.size.height = 216;
self.functionView.frame = frame;
self.moreView.frame = frame;
}
//橫屏
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
CGRect frame = self.functionView.frame;
frame.size.height = 150;
self.functionView.frame = frame;
self.moreView.frame = frame;
}
}
4.當鍵盤出來的時刻,轉變toolView的地位,經由過程鍵盤的告訴來完成。當橫屏的時刻鍵盤的坐標系和我們以後的Frame的坐標系紛歧樣所以當橫屏時得做一坐標系的轉換,代碼以下:
//當鍵盤出來的時刻轉變toolView的地位(接到鍵盤出來的告訴要做的辦法)
-(void) keyNotification : (NSNotification *) notification
{
NSLog(@"%@", notification.userInfo);
self.keyBoardDic = notification.userInfo;
//獲得鍵盤挪動後的坐標點的坐標點
CGRect rect = [self.keyBoardDic[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//把鍵盤的坐標系改成以後我們window的坐標系
CGRect r1 = [self.view convertRect:rect fromView:self.view.window];
[UIView animateWithDuration:[self.keyBoardDic[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
//動畫曲線
[UIView setAnimationCurve:[self.keyBoardDic[UIKeyboardAnimationCurveUserInfoKey] doubleValue]];
CGRect frame = self.toolView.frame;
frame.origin.y = r1.origin.y - frame.size.height;
//依據鍵盤的高度來轉變toolView的高度
self.toolView.frame = frame;
}];
}
5.體系鍵盤和自界說鍵盤切換的代碼以下:
//切換鍵盤的辦法
-(void) changeKeyboardToFunction
{
if ([self.myTextView.inputView isEqual:self.functionView])
{
self.myTextView.inputView = nil;
[self.myTextView reloadInputViews];
}
else
{
self.myTextView.inputView = self.functionView;
[self.myTextView reloadInputViews];
}
if (![self.myTextView isFirstResponder])
{
[self.myTextView becomeFirstResponder];
}
}
以上就是下面展現後果的焦點代碼了,在做的時刻感到難點在於若何停止屏幕適配,特別是當屏幕橫過去的時刻鍵盤的坐標系和我們frame的坐標系分歧,得做 一個轉換。揭橥文章的目標是想起到拋磚引玉的閣下,有好的器械願望年夜家互相交換一下。
【iOS組件封裝與主動結構自界說臉色鍵盤】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!