国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS實現(xiàn)類似微信和支付寶的密碼輸入框(UIKeyInput協(xié)議)

2019-10-21 18:53:25
字體:
供稿:網(wǎng)友

目前在項目中需要實現(xiàn)發(fā)紅包的功能,自己就寫了一個密碼輸入框的控件,主要用到了UIKeyInput協(xié)議和CoreGraphics框架,效果類似微信支付,感覺還行就把我的思路和制作過程寫下來給大家分享一下。

ios微信輸入框?qū)崿F(xiàn),ios密碼輸入框,ios微信密碼輸入框,ios支付寶密碼輸入框

讓你的自定義View具備輸入的功能(UIKeyInput協(xié)議)

通過UIKeyInput協(xié)議可以為響應(yīng)者提供簡單的鍵盤輸入的功能,讓需要鍵盤的responder成為第一響應(yīng)者就行了。UIKeyInput協(xié)議必須實現(xiàn)的有三個方法,分別是以下方法:

#pragma mark - UIKeyInput/** * 用于顯示的文本對象是否有任何文本 */- (BOOL)hasText {  return self.textStore.length > 0;}/** * 插入文本 */- (void)insertText:(NSString *)text {  if (self.textStore.length < self.passWordNum) {    //判斷是否是數(shù)字    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];}/** * 是否能成為第一響應(yīng)者 */- (BOOL)canBecomeFirstResponder {  return YES;}/** * 點擊成為第一相應(yīng)者 */- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {  if (![self isFirstResponder]) {    [self becomeFirstResponder];  }}

通過CoreGraphics繪制出密碼輸入框

實現(xiàn)的思路是通過CoreGraphics框架繪制出密碼輸入框的外框和里面的小黑點,然后通過從鍵盤上獲取到的字符串判斷輸入的位數(shù),具體實現(xiàn)如下:

/** * 設(shè)置正方形的邊長 */- (void)setSquareWidth:(CGFloat)squareWidth {  _squareWidth = squareWidth;  [self setNeedsDisplay];}/** * 設(shè)置鍵盤的類型 */- (UIKeyboardType)keyboardType {  return UIKeyboardTypeNumberPad;}/** * 設(shè)置密碼的位數(shù) */- (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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐至县| 时尚| 绥中县| 随州市| 孙吴县| 庆元县| 台东县| 京山县| 普定县| 新疆| 绵竹市| 福州市| 耒阳市| 固始县| 扶绥县| 长丰县| 贡嘎县| 博爱县| 广丰县| 合肥市| 射洪县| 嘉峪关市| 铜梁县| 临漳县| 延吉市| 玉门市| 营口市| 揭东县| 贵定县| 海盐县| 栾川县| 新乡市| 霸州市| 库尔勒市| 固安县| 宜良县| 科尔| 新乐市| 辽中县| 久治县| 鹤岗市|