This commit is contained in:
MengLiMing
2018-11-13 11:18:08 +08:00
parent 0b42267347
commit dd05feccb9
5 changed files with 73 additions and 71 deletions

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>CodeView.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@@ -17,6 +17,7 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
//输入完成回调 //输入完成回调
@property (nonatomic, copy) void(^EndEditBlcok)(NSString *text); @property (nonatomic, copy) void(^EndEditBlcok)(NSString *text);
@property (nonatomic, copy) void(^changeEditBlock)();
//样式 //样式
@property (nonatomic, assign) CodeViewType codeType; @property (nonatomic, assign) CodeViewType codeType;
@@ -29,6 +30,7 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
//是否需要输入之后清空,再次输入使用,默认为NO //是否需要输入之后清空,再次输入使用,默认为NO
@property (nonatomic, assign) BOOL emptyEditEnd; @property (nonatomic, assign) BOOL emptyEditEnd;
//是否添加下划线的动画,默认NO //是否添加下划线的动画,默认NO
@property (nonatomic, assign) BOOL underLineAnimation; @property (nonatomic, assign) BOOL underLineAnimation;
@@ -40,6 +42,8 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
//下划线中心点 //下划线中心点
@property (nonatomic, assign) CGFloat underLine_center_y; @property (nonatomic, assign) CGFloat underLine_center_y;
@property (nonatomic, strong) NSString *text;
- (instancetype)initWithFrame:(CGRect)frame - (instancetype)initWithFrame:(CGRect)frame
num:(NSInteger)num num:(NSInteger)num
@@ -48,5 +52,6 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
- (void)beginEdit; - (void)beginEdit;
- (void)endEdit; - (void)endEdit;
- (void)emptyCodeView;
@end @end

View File

@@ -19,22 +19,18 @@
// //
#define RADIUS 5 #define RADIUS 5
@interface CodeView () <UITextFieldDelegate> @interface CodeView ()
{ {
NSMutableArray *textArray; NSMutableArray *textArray;
//线 //线
NSInteger lineNum; NSInteger lineNum;
UIColor *linecolor; UIColor *linecolor;
UIColor *textcolor; UIColor *textcolor;
UIFont *textFont; UIFont *textFont;
//
NSObject *observer;
} }
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic, strong) NSMutableArray *underlineArr; @property (nonatomic, strong) NSMutableArray *underlineArr;
@end @end
@@ -60,20 +56,14 @@
textFont = [UIFont boldSystemFontOfSize:font]; textFont = [UIFont boldSystemFontOfSize:font];
_text = @"";
_underLine_center_y = frame.size.height - LineBottomHeight - LineHeight/2; _underLine_center_y = frame.size.height - LineBottomHeight - LineHeight/2;
self.textField.delegate = self;
_underLineAnimation = NO; _underLineAnimation = NO;
_emptyEditEnd = NO; _emptyEditEnd = NO;
//self //self
NSAssert(textFont.lineHeight < self.frame.size.height, @"设置的字体高度应该小于self的高"); NSAssert(textFont.lineHeight < self.frame.size.height, @"设置的字体高度应该小于self的高");
//
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(beginEdit)];
[self addGestureRecognizer:tapGes];
} }
return self; return self;
@@ -102,56 +92,46 @@
} }
#pragma mark -
- (void)addNotification {
//bug
if (observer) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
// - (void)setText:(NSString *)text {
textArray = [[self charArray:text] mutableCopy];
_text = [textArray componentsJoinedByString:@""];
//
[self setNeedsDisplay];
[self underLineHidden];
[self addUnderLineAnimation]; [self addUnderLineAnimation];
observer = [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { if (textArray.count < lineNum) {
NSInteger length = _textField.text.length; if (self.changeEditBlock) {
self.changeEditBlock();
//
//textfield
if (length > textArray.count) {
[textArray addObject:[_textField.text substringWithRange:NSMakeRange(textArray.count, 1)]];
} else {
[textArray removeLastObject];
} }
} else if (textArray.count == lineNum) {
// if (self.EndEditBlcok) {
[self setNeedsDisplay];
[self underLineHidden];
[self addUnderLineAnimation];
if (length == lineNum && self.EndEditBlcok) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.EndEditBlcok(_textField.text); self.EndEditBlcok(_text);
[self emptyAndDisplay];
}); });
} }
if (length > lineNum) { }
_textField.text = [_textField.text substringToIndex:lineNum]; }
[self emptyAndDisplay];
}
}]; - (NSArray *)charArray:(NSString *)text {
NSMutableArray *array = [NSMutableArray array];
NSInteger max = MIN(text.length, lineNum);
for (NSInteger i = 0; i < max; i ++) {
NSString *str = [text substringWithRange:NSMakeRange(i, 1)];
[array addObject:str];
}
return array;
} }
// //
- (void)emptyAndDisplay { - (void)emptyAndDisplay {
[self endEdit]; _text = @"";
[textArray removeAllObjects];
if (_emptyEditEnd) { if (_emptyEditEnd) {
_textField.text = @"";
[textArray removeAllObjects];
[self setNeedsDisplay]; [self setNeedsDisplay];
[self underLineHidden]; [self underLineHidden];
} }
@@ -159,9 +139,19 @@
if (_noInputAni) { if (_noInputAni) {
[self addUnderLineAnimation]; [self addUnderLineAnimation];
} }
} }
- (void)emptyCodeView {
_text = @"";
[textArray removeAllObjects];
[self setNeedsDisplay];
[self underLineHidden];
if (_noInputAni) {
[self addUnderLineAnimation];
}
}
#pragma mark - 线 #pragma mark - 线
- (void)underLineHidden { - (void)underLineHidden {
@@ -177,28 +167,15 @@
// //
- (void)beginEdit { - (void)beginEdit {
if (_textField == nil) { [self addUnderLineAnimation];
_textField = [[UITextField alloc] init];
_textField.keyboardType = UIKeyboardTypeNumberPad;
_textField.hidden = YES;
_textField.delegate = self;
[self addSubview:_textField];
}
[self addNotification];
[self.textField becomeFirstResponder];
} }
- (void)endEdit { - (void)endEdit {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
[_underlineArr makeObjectsPerformSelector:@selector(removeAnimationForKey:) withObject:@"kOpacityAnimation"]; [_underlineArr makeObjectsPerformSelector:@selector(removeAnimationForKey:) withObject:@"kOpacityAnimation"];
[self.textField resignFirstResponder];
} }
#pragma mark - textfield
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self endEdit];
}
- (void)setUnderLine_center_y:(CGFloat)underLine_center_y { - (void)setUnderLine_center_y:(CGFloat)underLine_center_y {
@@ -230,7 +207,7 @@
if (!_noInputAni) { if (!_noInputAni) {
[self addUnderLineAnimation]; [self addUnderLineAnimation];
} }
} }
//线 //线
@@ -263,7 +240,7 @@
{ {
// //
// //
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextRef context = UIGraphicsGetCurrentContext();
for (NSInteger i = 0; i < textArray.count; i ++) { for (NSInteger i = 0; i < textArray.count; i ++) {
NSString *num = textArray[i]; NSString *num = textArray[i];
@@ -294,11 +271,9 @@
default: default:
break; break;
} }
} }
#pragma mark - 线,线 #pragma mark - 线,线
- (void)addUnderLineAnimation { - (void)addUnderLineAnimation {
if (_underLineAnimation) { if (_underLineAnimation) {