ios 第三方键盘开发 无限刷屏浅析

时间:2019-02-19
本文章向大家介绍ios 第三方键盘开发 无限刷屏浅析,主要包括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