iOS は WeChat や Alipay と同様のパスワード入力ボックスを実装します (UIKeyInput プロトコル)
現在、プロジェクトに赤い封筒を送信する機能を実装する必要があるので、主に UIKeyInput プロトコルと CoreGraphics フレームワークを使用して、パスワード入力ボックス コントロールを作成しました。効果が WeChat 支払いに似ていると感じたら、実行します。私のアイデアと制作プロセスを書き留めておきます。
カスタム ビューに入力機能 (UIKeyInput プロトコル) を持たせます
UIKeyInput プロトコルは、レスポンダーに簡単なキーボード入力機能を提供し、キーボードを必要とするレスポンダーをファーストレスポンダーにさせるだけです。 UIKeyInput プロトコルで実装する必要があるメソッドは 3 つあり、次のメソッドです:
#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); 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); } }
以上がこの記事の内容です。皆様の学習に役立つとともに、皆様にも PHP 中国語 Web サイトをサポートしていただければ幸いです。
WeChat や Alipay 関連の記事と同様のパスワード入力ボックス (UIKeyInput プロトコル) の iOS 実装については、PHP 中国語 Web サイトに注目してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









