今朝在項目中須要完成發紅包的功效,本身就寫了一個暗碼輸出框的控件,重要用到了UIKeyInput協定和CoreGraphics框架,後果相似微信付出,感到還行就把我的思緒和制造進程寫上去給年夜家分享一下。

讓你的自界說View具有輸出的功效(UIKeyInput協定)
經由過程UIKeyInput協定可認為呼應者供給簡略的鍵盤輸出的功效,讓須要鍵盤的responder成為第一呼應者就好了。UIKeyInput協定必需完成的有三個辦法,分離是以下辦法:
#pragma mark - UIKeyInput
/**
* 用於顯示的文本對象能否有任何文本
*/
- (BOOL)hasText {
return self.textStore.length > 0;
}
/**
* 拔出文本
*/
- (void)insertText:(NSString *)text {
if (self.textStore.length < self.passWordNum) {
//斷定能否是數字
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:MONEYNUMBERS] invertedSet];
NSString*filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [text isEqualToString:filtered];
if(basicTest) {
if ([self.delegate respondsToSelector:@selector(passWordDidChange:)]) {
[self.delegate passWordDidChange:self];
}
if (self.textStore.length == self.passWordNum) {
if ([self.delegate respondsToSelector:@selector(passWordCompleteInput:)]) {
[self.delegate passWordCompleteInput:self];
}
}
[self.textStore appendString:text];
[self setNeedsDisplay];
}
}
}
/**
* 刪除文本
*/
- (void)deleteBackward {
if (self.textStore.length > 0) {
[self.textStore deleteCharactersInRange:NSMakeRange(self.textStore.length - 1, 1)];
if ([self.delegate respondsToSelector:@selector(passWordDidChange:)]) {
[self.delegate passWordDidChange:self];
}
}
[self setNeedsDisplay];
}
/**
* 能否能成為第一呼應者
*/
- (BOOL)canBecomeFirstResponder {
return YES;
}
/**
* 點擊成為第一響應者
*/
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (![self isFirstResponder]) {
[self becomeFirstResponder];
}
}
經由過程CoreGraphics繪制出暗碼輸出框
完成的思緒是經由過程CoreGraphics框架繪制出暗碼輸出框的外框和外面的小斑點,然後經由過程從鍵盤上獲得到的字符串斷定輸出的位數,詳細完成以下:
/**
* 設置正方形的邊長
*/
- (void)setSquareWidth:(CGFloat)squareWidth {
_squareWidth = squareWidth;
[self setNeedsDisplay];
}
/**
* 設置鍵盤的類型
*/
- (UIKeyboardType)keyboardType {
return UIKeyboardTypeNumberPad;
}
/**
* 設置暗碼的位數
*/
- (void)setPassWordNum:(NSUInteger)passWordNum {
_passWordNum = passWordNum;
[self setNeedsDisplay];
}
/**
* 繪制
*/
- (void)drawRect:(CGRect)rect {
CGFloat height = rect.size.height;
CGFloat width = rect.size.width;
CGFloat x = (width - self.squareWidth*self.passWordNum)/2.0;
CGFloat y = (height - self.squareWidth)/2.0;
CGContextRef context = UIGraphicsGetCurrentContext();
//畫外框
CGContextAddRect(context, CGRectMake( x, y, self.squareWidth*self.passWordNum, self.squareWidth));
CGContextSetLineWidth(context, 1);
CGContextSetStrokeColorWithColor(context, self.rectColor.CGColor);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
//畫豎條
for (int i = 1; i <= self.passWordNum; i++) {
CGContextMoveToPoint(context, x+i*self.squareWidth, y);
CGContextAddL.netoPoint(context, x+i*self.squareWidth, y+self.squareWidth);
CGContextClosePath(context);
}
CGContextDrawPath(context, kCGPathFillStroke);
CGContextSetFillColorWithColor(context, self.pointColor.CGColor);
//畫斑點
for (int i = 1; i <= self.textStore.length; i++) {
CGContextAddArc(context, x+i*self.squareWidth - self.squareWidth/2.0, y+self.squareWidth/2, self.pointRadius, 0, M_PI*2, YES);
CGContextDrawPath(context, kCGPathFill);
}
}
源碼下載:https://github.com/631106979/WCLPassWordView
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐本站。
【iOS完成相似微信和付出寶的暗碼輸出框(UIKeyInput協定)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!