In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "iOS CAPTCHA input train of thought analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "iOS CAPTCHA input thinking analysis" it!
1. Composed of multiple UITextField
The advantage of this approach is that there is cursor flicker, but it will be a bit clumsy when dealing with deletions and animation effects. OFO should be implemented in this way, strictly dealing with the FirstResponder of each UITextField.
two。 Composed of a UITextField, using rich text
This approach is feasible, use rich text to set the spacing of each character, allow editing rich text, there is cursor flicker, the disadvantage should also be bad to deal with animation effects.
3. Draw with UIView
This is a semi-finished Demo I saw on GitHub, using a UIView, using Quartz 2D and UIBezierPath to draw text and underline, and dealing with input events and keyboard events, in fact, the overall code is not much, less than 300 lines, but requires good iOS drawing skills.
4. One UITextField and multiple UILabel
This is the idea I will introduce next. The disadvantage of this idea should be that there is no cursor flicker. In fact, it can also be pseudo-implemented to see if it is necessary to have this need. This idea is relatively simple, easy to add animation, purely more than 100 lines, then look at the structure and code:
Create a new UIView and initialize the method
-(instancetype) initWithCount: (NSInteger) count margin: (CGFloat) margin {if (self = [super init]) {self.itemCount = count; / / itemCount is the number of verification codes, for example, 6 self.itemMargin = margin; / / itemMargin is the distance between each Label [self configTextField];} return self;}
Add internal child controls (demo)
-(void) configTextField {UITextField * textField = [[UITextField alloc] init]; textField.autocapitalizationType = UITextAutocapitalizationTypeNone; textField.keyboardType = UIKeyboardTypeNumberPad; [textField addTarget:self action:@selector (tfEditingChanged:) forControlEvents: (UIControlEventEditingChanged)]; / / Tip: this property is YES and can be forced to use the system's numeric keypad. The disadvantage is that the previous content / / clearsOnBeginEditing attribute is cleared when re-typed and does not apply to secureTextEntry = YES / / textField.secureTextEntry = YES; [self addSubview:textField] Self.textField = textField; / / Tip: by covering a maskView on the top layer of textField, you can remove the long press event of textField UIButton * maskView = [UIButton new]; maskView.backgroundColor = [UIColor whiteColor]; [maskView addTarget:self action:@selector (clickMaskView) forControlEvents: (UIControlEventTouchUpInside)]; [self addSubview:maskView]; self.maskView = maskView; for (NSInteger I = 0; I)
< self.itemCount; i++) { UILabel *label = [UILabel new]; [self addSubview:label]; [self.labels addObject:label]; } for (NSInteger i = 0; i < self.itemCount; i++) { UIView *line = [UIView new]; [self.lines addObject:line]; }} 这里可能对maskView有点费解,maskView主要是为了挡住下面的UITextField,使用类UIButton是为了挡住事件,因为如果使用类UIView,会将事件传递到self,进而影响到外面隐藏键盘的代码,你可以试试就知道了。 主要处理业务逻辑的代码 #pragma mark - 编辑改变- (void)tfEditingChanged:(UITextField *)textField{ if (textField.text.length >Self.itemCount) {textField.text = [textField.text substringWithRange:NSMakeRange (0, self.itemCount)];} for (int I = 0; I
< self.itemCount; i++) { UILabel *label = [self.labels objectAtIndex:i]; if (i < textField.text.length) { label.text = [textField.text substringWithRange:NSMakeRange(i, 1)]; } else { label.text = nil; } } // 输入完毕后,自动隐藏键盘 if (textField.text.length >= self.itemCount) {[textField resignFirstResponder];}}
There is no delegate using UITextField, so using UIControlEventEditingChanged is enough, but if your requirements can enter other characters such as English, you need to implement delegate to limit it.
At this point, the core code of CAPTCHA input is gone, does it feel so little! On this basis, I implemented three basic effects with Demo, as shown in the figure:
Github code address: click me
Tip Tip
When you don't want others to use a method, you can do this in addition to proprietary methods:
-(instancetype) init UNAVAILABLE_ATTRIBUTE;+ (instancetype) new UNAVAILABLE_ATTRIBUTE
Thank you for your reading, the above is the content of "iOS CAPTCHA input implementation train of thought analysis". After the study of this article, I believe you have a deeper understanding of the problem of iOS CAPTCHA input implementation train of thought analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.