iOS仿工商銀行app自定義鍵盤

近日項(xiàng)目需求,需要做自定義鍵盤,因?yàn)轫?xiàng)目對(duì)安全性能要求較高,所以摒棄了系統(tǒng)鍵盤和一系列的第三方鍵盤,仿照工商銀行app的鍵盤寫了一個(gè)自定義鍵盤的demo,因?yàn)橹坝泻芏嗟那拜吜粝伦阚E,所以做起來(lái)也不是很費(fèi)力,在這里要感謝所有知識(shí)分享者,我不是巨人,我只是知識(shí)的搬運(yùn)工,言歸正傳,在本demo里面,鍵盤布局并沒(méi)有使用xib和storyboard,純代碼布局,至于因?yàn)槭裁?。。。。。。我想你懂得!先?lái)看一下效果圖,請(qǐng)自動(dòng)忽略比較丑的樣子,主要看實(shí)現(xiàn)思路

自定義鍵盤

想讓UITextView彈出自己的鍵盤而不是系統(tǒng)鍵盤其實(shí)很簡(jiǎn)單,只需要一句代碼即可

self.textView.inputView= keyboard;

剩下的就是自定義鍵盤view,分幾個(gè)部分:

1、鍵盤類型切換按鈕布局

//創(chuàng)建三種鍵盤切換button

- (void)configKeyboardType

{

? ? NSIntegerbtnTypeCount =3;

? ? CGFloatbtnWidth = (DeviceWidth/2)/btnTypeCount;

? ? CGFloatbtnHeight =20;

? ? NSArray*titles =@[@"數(shù)字",@"字母",@"符號(hào)"];

? ? for(NSIntegeri =0; i < btnTypeCount; i++)

? ? {

? ? ? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

? ? ? ? btn.frame=CGRectMake((DeviceWidth/2)+i*btnWidth,5, btnWidth, btnHeight);

? ? ? ? [btnsetTitle:titles[i] forState:UIControlStateNormal];

? ? ? ? [btnsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

? ? ? ? [btnsetTitleColor:[UIColor blueColor] forState:UIControlStateSelected];

? ? ? ? btn.tag= (CustomKeyboardType)i+1;

? ? ? ? if(i ==1)

? ? ? ? {

? ? ? ? ? ? //默認(rèn)字母鍵盤

? ? ? ? ? ? btn.selected=YES;

? ? ? ? }

? ? ? ? [btnaddTarget:self action:@selector(typeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

? ? ? ? [selfaddSubview:btn];

? ? }

}

//點(diǎn)擊切換鍵盤

- (void)typeBtnClicked:(UIButton*)sender

{

? ? if(sender.selected)

? ? {

? ? ? ? return;

? ? }

? ? UIButton *btn_num = [self viewWithTag:CustomKeyboardType_Num];

? ? UIButton *btn_let = [self viewWithTag:CustomKeyboardType_Letters];

? ? UIButton *btn_sym = [self viewWithTag:CustomKeyboardType_Symbol];

? ? sender.selected=YES;

? ? if(sender == btn_num)

? ? {

? ? ? ? btn_let.selected=NO;

? ? ? ? btn_sym.selected=NO;

? ? ? ? [self clickNumTypeBtn];

? ? }

? ? if(sender == btn_let)

? ? {

? ? ? ? btn_num.selected=NO;

? ? ? ? btn_sym.selected=NO;

? ? ? ? [self clickLetterTypeBtn];

? ? }

? ? if(sender == btn_sym)

? ? {

? ? ? ? btn_num.selected=NO;

? ? ? ? btn_let.selected=NO;

? ? ? ? [self clickSymbleTypeBtn];

? ? }

}

2、三種鍵盤布局,也是采用的自定義view來(lái)封裝

[self configNumKeyBoard];

[self configLetterKeyboard];

[self configSymbolKeyboard];

三種鍵盤全部繼承一個(gè)基類PwKeyboardBaseView,里面包含了點(diǎn)擊普通按鍵、刪除按鍵、確認(rèn)按鍵的block回調(diào)

@property(nonatomic,copy)void(^btnClickedCallback)(UIButton*btn);

@property(nonatomic,copy)void(^deleteBtnClickedCallback)(void);

@property(nonatomic,copy)void(^returnBtnClickedCallback)(void);

其中數(shù)字鍵盤做了亂序,只需要將裝載所有數(shù)字的數(shù)組做亂序即可

_allNums = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];

? ? _allNums = [_allNums sortedArrayUsingComparator:^NSComparisonResult(id? _Nonnull obj1, id? _Nonnull obj2) {

? ? ? ? intseed =arc4random_uniform(2);

? ? ? ? if(seed) {

? ? ? ? ? ? return[obj1compare:obj2];

? ? ? ? }else{

? ? ? ? ? ? return[obj2compare:obj1];

? ? ? ? }

? ? }];

思路就是將所有的數(shù)字、字母、符號(hào)全裝在一個(gè)數(shù)組之中,再按行來(lái)拆分成若干數(shù)組,作為按鈕標(biāo)題

代碼比較簡(jiǎn)單,也不再多介紹

最后附上demo地址:demo地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容