專案裡要做發紅包功能,於是動手寫了一個密碼輸入框控制項,主要用到 UIKeyInput 協議與 CoreGraphics 框架,成品效果類似微信支付。這裡把我的思路與實作過程記錄下來分享。

一、讓自訂 View 具備輸入能力(UIKeyInput 協議)
UIKeyInput 協議能為響應者提供最簡單的鍵盤輸入能力,只要讓需要的 responder 成為第一響應者即可。協議中有三個必須實作的方法:
#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 畫出外框、分隔線與已輸入位置的小黑點,再依 textStore 的長度決定要畫幾個點。
/** * 設定正方形邊長 */ - (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); CGContextAddLineToPoint(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
補一個容易忽略的細節:因為鍵盤類型設為 UIKeyboardTypeNumberPad,系統不會提供任何完成或確認鍵,所以實務上通常還要自己加一個自訂的鍵盤工具列(或用 UITextField 的 inputView)來放「確認」按鈕,否則使用者輸入完只能盲猜。以上就是這個自訂密碼輸入框的完整思路,希望對大家有幫助。