ios 第三方键盘开发 无限刷屏浅析
?
姬拉 關注
0.1 2016.09.02 19:26* 字數 629 閱讀 958評論 2喜歡 4
故事前景:
? 某直播刷人氣,主播隨機截圖抽取當前留言者為中獎對象......
?想著寫個第三方鍵盤無限刷 ?然后寫了這個么小玩意,貼出來互相學習
首先當然是創建一個手機APP項目:(這一步貼上來真是浪費流量.....)
?
新建項目以后我直接新建了兩個控件:(為了研究鍵盤的發送,和寫入數據用)
?
?
一開始我以為蘋果會直接提供發送方法,類似點擊鍵盤上的(done,return....)找了一圈蘋果文檔么發現,后來去墻外發現了原來是換一種方式玩的,往下看:
在原來的項目上添加了一個 target 如圖 (這玩意就是蘋果的第三方鍵盤了)
?
?
如下是自動創建的兩個類,所有內容都從這里面展開:
?
首先要解決的第一個問題是 如何插入文字,以微信為例,如何在微信聊天輸入框插入文本:
UITextDocumentProxy 這個類為我們提供了插入,刪除,等基本操作,但是方法也不多就幾個基本的
//自己猜
- (void)dismissKeyboard;
//切換鍵盤
- (void)advanceToNextInputMode;
//判斷是否存在文本(我猜的沒有試驗)
- (BOOL)hasText;
//插入文本
- (void)insertText:(NSString*)text;
//刪除(我估計刪除一個字符,我猜的沒試)
- (void)deleteBackward;
?
這些方法夠用了,一開始跑起來時候默認是長這樣的:
?
?
創建新的view 沒有什么特別的
?
跑起來的時候讓我選擇個APP 執行 但是我發現 沒有選的也可以使用鍵盤,這個我后期研究差不多了陸續提交:
?
最最最重點的是 我們已經可以插入文字,接下來就是解決如何調用發送這個功能了,找了一圈文檔沒找到,后來聽了一遍 《流川楓與蒼井空》豁然開朗,原來只要插入 @“\n” 就能調用 別人APP的
-(BOOL)textFieldShouldReturn:(UITextField*)textField
這個方法。
//發送
[self.textDocumentProxyinsertText:@"\n"];
?
接下來的刷屏就簡單了,一個定時 一插一發 ?一發一插.....無窮盡也。
配上幾個效果圖:
?
?
end;
本人 課余喜歡研究? object pascal ,object c ,java,玩些 c# 。歡迎廣大IT男加群 (367276878)互相學習
大家好我叫姬拉
?
iOS 自定義第三方鍵盤
28畫生 關注
2017.05.03 15:56* 字數 434 閱讀 1099評論 4喜歡 1
iOS8新特性擴展(Extension)應用之四——自定義鍵盤控件
iOS8系統的開放第三方鍵盤,使得用戶在輸入法的選擇上更加自主靈活,也更加貼近不同語言的輸入風格。這篇博客,將介紹如何開發一個第三方的鍵盤控件。
一、了解UIInputViewController類
UIInputViewController是系統擴展支持鍵盤擴展的一個類,通過這個類,我們可以自定義一款我們自己的鍵盤提供給系統使用。
首先,我們先來看一下這個類中的一些屬性和方法:
@property (nonatomic, retain) UIInputView *inputView;
鍵盤的輸入視圖,我們可以自定義這個視圖。
@property (nonatomic, readonly) NSObject <UITextDocumentProxy> *textDocumentProxy;
實現了UITextDocumentProxy協議的一個對象,后面會介紹這個協議。
@property (nonatomic, copy) NSString *primaryLanguage;
系統為我們準備了一些本地化的語言字符串
- (void)dismissKeyboard;
收鍵盤的方法
- (void)advanceToNextInputMode;
切換到下一輸入法的方法
UITextDocumentProxy協議內容如下:
?
@protocol UITextDocumentProxy <UIKeyInput>
//輸入的上一個字符
@property (nonatomic, readonly) NSString *documentContextBeforeInput;
//即將輸入的一個字符
@property (nonatomic, readonly) NSString *documentContextAfterInput;
//將輸入的字符移動到某一位置
- (void)adjustTextPositionByCharacterOffset:(NSInteger)offset;
?
@end
而UITextDocumentProxy這個協議繼承與UIKeyInput協議,UIKeyInput協議中提供的兩個方法用于輸入字符和刪除字符:
- (void)insertText:(NSString *)text;
- (void)deleteBackward;
二、創建一款最簡單的數字輸入鍵盤
? ? 創建一個項目,作為宿主APP,接著我們File->new->target->customKeyBoard:
?
Snip20170503_8.png
?
Snip20170503_9.png
系統要求我們對鍵盤的布局要使用autolayout,并且只可以采用代碼布局的方式,我們這里為了簡單演示,將坐標寫死:
- (void)viewDidLoad {
? ? [super viewDidLoad];
?? ?
? ? // 設置數字鍵盤的UI
? ? //數字按鈕布局
? ? for (int i=0; i<10; i++) {
? ? ? ? UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
? ? ? ? btn.frame=CGRectMake(20+45*(i%3), 20+45*(i/3), 40, 40);
? ? ? ? btn.backgroundColor=[UIColor greenColor];
? ? ? ? [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
? ? ? ? btn.tag=101+i;
? ? ? ? [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? [self.view addSubview:btn];
? ? }
? ? //創建切換鍵盤按鈕
? ? UIButton * change = [UIButton buttonWithType:UIButtonTypeSystem];
? ? change.frame=CGRectMake(200,20, 80, 40) ;
? ? NSLog(@"%f,%f",self.view.frame.size.height,self.view.frame.size.width);
? ? [change setBackgroundColor:[UIColor blueColor]];
? ? [change setTitle:@"切換鍵盤" forState:UIControlStateNormal];
? ? [change addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:change];
? ? //創建刪除按鈕
? ? UIButton * delete = [UIButton buttonWithType:UIButtonTypeSystem];
? ? delete.frame=CGRectMake(200, 120, 80, 40);
? ? [delete setTitle:@"delete" forState:UIControlStateNormal];
? ? [delete setBackgroundColor:[UIColor redColor]];
? ? [delete addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:delete];
}
//刪除方法
-(void)delete{
? ? if (self.textDocumentProxy.documentContextBeforeInput) {
? ? ? ? [self.textDocumentProxy deleteBackward];
? ? }
}
//切換鍵盤方法
-(void)change{
? ? [self advanceToNextInputMode];
}
//點擊數字按鈕 將相應數字輸入
-(void)click:(UIButton *)btn{
? ? [self.textDocumentProxy insertText:[NSString stringWithFormat:@"%ld",btn.tag-101]];
}
運行后,在使用之前,我們需要先加入這個鍵盤:在模擬器系統設置中general->keyboard->keyboards->addNowKeyboard
選中我們自定義的鍵盤,之后運行瀏覽器,切換到我們的鍵盤,效果如下:
?
?
//
//? KeyboardViewController.m
//? KeyboardViewController
//
//? Created by 王彥平 on 2019/2/18.
//? Copyright ? 2019年 王彥平. All rights reserved.
//
?
#import "KeyboardViewController.h"
?
@interface KeyboardViewController (){
? ? UIButton *godoit;
}
@property (nonatomic, strong) UIButton *nextKeyboardButton;
@end
?
@implementation KeyboardViewController
?
- (void)updateViewConstraints {
? ? [super updateViewConstraints];
?? ?
? ? // Add custom view sizing constraints here
}
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
?? ?
? ? // Perform custom UI setup here
? ? self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];
?? ?
? ? [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
? ? [self.nextKeyboardButton sizeToFit];
? ? self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;
?? ?
? ? [self.nextKeyboardButton addTarget:self action:@selector(handleInputModeListFromView:withEvent:) forControlEvents:UIControlEventAllTouchEvents];
?? ?
? ? [self.view addSubview:self.nextKeyboardButton];
?? ?
? ? [self.nextKeyboardButton.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
? ? [self.nextKeyboardButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
?
?? ?
? ? [self createuI];
}
?
- (void)textWillChange:(id<UITextInput>)textInput {
? ? // The app is about to change the document's contents. Perform any preparation here.
}
?
- (void)textDidChange:(id<UITextInput>)textInput {
? ? // The app has just changed the document's contents, the document context has been updated.
?? ?
? ? UIColor *textColor = nil;
? ? if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
? ? ? ? textColor = [UIColor whiteColor];
? ? } else {
? ? ? ? textColor = [UIColor blackColor];
? ? }
? ? [self.nextKeyboardButton setTitleColor:textColor forState:UIControlStateNormal];
}
-(void)createuI{
? ? godoit = [UIButton buttonWithType:UIButtonTypeCustom];
? ? godoit.backgroundColor = [UIColor blueColor];
? ? godoit.frame = CGRectMake(60, 100, 100, 100);
? ? [godoit setTitle:@"走你" forState:UIControlStateNormal];
? ? [godoit setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
? ? [godoit addTarget:self action:@selector(doit:) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:godoit];
?? ?
? ? UIButton *btn2? = [UIButton buttonWithType:UIButtonTypeCustom];
? ? btn2.backgroundColor = [UIColor blueColor];
? ? btn2.frame = CGRectMake(200, 100, 100, 100);
? ? [btn2 setTitle:@"變態循環" forState:UIControlStateNormal];
? ? [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//? ? [btn2 addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:btn2];
?? ?
?
}
-(void)doit:(UIButton *)sender{
? ? [self.textDocumentProxy insertText:@"\n"];
?
? ? [self.textDocumentProxy insertText:@"你好我叫基拉"];
}
?
@end
?
#import "ViewController.h"
?
@interface ViewController ()<UITextViewDelegate,UITextFieldDelegate>
?
@end
?
@implementation ViewController
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? UITextView *text = [[UITextView alloc]initWithFrame:CGRectMake(0, 340, self.view.bounds.size.width, 300)];
? ? text.backgroundColor = [UIColor colorWithRed:0.8304 green:0.8304 blue:0.8304 alpha:1.0];
? ? [self.view addSubview:text];
? ? text.delegate = self;
?? ?
? ? UITextField *text2 = [[UITextField alloc]initWithFrame:CGRectMake(0, 30, self.view.bounds.size.width, 30)];
? ? text2.backgroundColor = [UIColor colorWithRed:0.8304 green:0.8304 blue:0.8304 alpha:1.0];
? ? [self.view addSubview:text2];
? ? text2.delegate = self;
}
?
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
? ? textField.text = [textField.text stringByAppendingString:@"done"];
? ? //發送
?? ?
? ? return YES;
}
-(void)textViewDidChange:(UITextView *)textView{
? ? textView.text = [textView.text stringByAppendingString:@""];
}
@end
?
#import "KeyboardViewController.h"
?
@interface KeyboardViewController (){
? ? UIButton *godoit;
}
?
@property (nonatomic, strong) UIButton *nextKeyboardButton;
@end
?
@implementation KeyboardViewController
?
- (void)updateViewConstraints {
? ? [super updateViewConstraints];
?? ?
? ? // Add custom view sizing constraints here
}
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
?? ?
? ? // Perform custom UI setup here
? ? self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];
?? ?
? ? [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
? ? [self.nextKeyboardButton sizeToFit];
? ? self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;
?? ?
? ? [self.nextKeyboardButton addTarget:self action:@selector(handleInputModeListFromView:withEvent:) forControlEvents:UIControlEventAllTouchEvents];
?? ?
? ? [self.view addSubview:self.nextKeyboardButton];
?? ?
? ? [self.nextKeyboardButton.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
? ? [self.nextKeyboardButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
? ? [self createuI];
?
}
?
- (void)textWillChange:(id<UITextInput>)textInput {
? ? // The app is about to change the document's contents. Perform any preparation here.
}
?
- (void)textDidChange:(id<UITextInput>)textInput {
? ? // The app has just changed the document's contents, the document context has been updated.
?? ?
? ? UIColor *textColor = nil;
? ? if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
? ? ? ? textColor = [UIColor whiteColor];
? ? } else {
? ? ? ? textColor = [UIColor blackColor];
? ? }
? ? [self.nextKeyboardButton setTitleColor:textColor forState:UIControlStateNormal];
}
-(void)createuI{
? ? godoit = [UIButton buttonWithType:UIButtonTypeCustom];
? ? godoit.backgroundColor = [UIColor blueColor];
? ? godoit.frame = CGRectMake(60, 100, 100, 100);
? ? [godoit setTitle:@"走你" forState:UIControlStateNormal];
? ? [godoit setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
? ? [godoit addTarget:self action:@selector(doit:) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:godoit];
?? ?
? ? UIButton *btn2? = [UIButton buttonWithType:UIButtonTypeCustom];
? ? btn2.backgroundColor = [UIColor blueColor];
? ? btn2.frame = CGRectMake(200, 100, 100, 100);
? ? [btn2 setTitle:@"變態循環" forState:UIControlStateNormal];
? ? [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
? ? [btn2 addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:btn2];
?? ?
?? ?
}
-(void)doit:(UIButton *)sender{
? ? [self.textDocumentProxy insertText:@"\n"];
?? ?
? ? [self.textDocumentProxy insertText:@"你好我叫基拉"];
}
?
-(void)send:(UIButton *)sender{
? ? [self.textDocumentProxy insertText:@"\n"];
?
}
@end
?
?
?
總結
以上是生活随笔為你收集整理的ios 第三方键盘开发 无限刷屏浅析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android记步传感器获取不到数据
- 下一篇: 即构音视频 Express Flutte