update
This commit is contained in:
@@ -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>
|
||||
BIN
CodeView.xcodeproj/project.xcworkspace/xcuserdata/mengliming.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
BIN
CodeView.xcodeproj/project.xcworkspace/xcuserdata/mengliming.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
Binary file not shown.
@@ -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>
|
||||
@@ -17,6 +17,7 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
|
||||
|
||||
//输入完成回调
|
||||
@property (nonatomic, copy) void(^EndEditBlcok)(NSString *text);
|
||||
@property (nonatomic, copy) void(^changeEditBlock)();
|
||||
|
||||
//样式
|
||||
@property (nonatomic, assign) CodeViewType codeType;
|
||||
@@ -29,6 +30,7 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
|
||||
//是否需要输入之后清空,再次输入使用,默认为NO
|
||||
@property (nonatomic, assign) BOOL emptyEditEnd;
|
||||
|
||||
|
||||
//是否添加下划线的动画,默认NO
|
||||
@property (nonatomic, assign) BOOL underLineAnimation;
|
||||
|
||||
@@ -40,6 +42,8 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
|
||||
//下划线中心点
|
||||
@property (nonatomic, assign) CGFloat underLine_center_y;
|
||||
|
||||
@property (nonatomic, strong) NSString *text;
|
||||
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
num:(NSInteger)num
|
||||
@@ -48,5 +52,6 @@ typedef NS_ENUM(NSInteger,CodeViewType) {
|
||||
|
||||
- (void)beginEdit;
|
||||
- (void)endEdit;
|
||||
- (void)emptyCodeView;
|
||||
|
||||
@end
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
//密码风格 圆点半径
|
||||
#define RADIUS 5
|
||||
|
||||
@interface CodeView () <UITextFieldDelegate>
|
||||
@interface CodeView ()
|
||||
{
|
||||
NSMutableArray *textArray;
|
||||
|
||||
@@ -30,11 +30,7 @@
|
||||
UIColor *textcolor;
|
||||
UIFont *textFont;
|
||||
|
||||
//观察者
|
||||
NSObject *observer;
|
||||
|
||||
}
|
||||
@property (nonatomic,strong) UITextField *textField;
|
||||
@property (nonatomic, strong) NSMutableArray *underlineArr;
|
||||
@end
|
||||
|
||||
@@ -60,20 +56,14 @@
|
||||
|
||||
textFont = [UIFont boldSystemFontOfSize:font];
|
||||
|
||||
|
||||
_text = @"";
|
||||
_underLine_center_y = frame.size.height - LineBottomHeight - LineHeight/2;
|
||||
|
||||
self.textField.delegate = self;
|
||||
|
||||
_underLineAnimation = NO;
|
||||
_emptyEditEnd = NO;
|
||||
//设置的字体高度小于self的高
|
||||
NSAssert(textFont.lineHeight < self.frame.size.height, @"设置的字体高度应该小于self的高");
|
||||
|
||||
//单击手势
|
||||
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(beginEdit)];
|
||||
[self addGestureRecognizer:tapGes];
|
||||
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -102,56 +92,46 @@
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - 添加通知
|
||||
- (void)addNotification {
|
||||
//修复双击造成的bug
|
||||
if (observer) {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:observer];
|
||||
}
|
||||
|
||||
//开始输入
|
||||
[self addUnderLineAnimation];
|
||||
|
||||
observer = [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
|
||||
NSInteger length = _textField.text.length;
|
||||
|
||||
//改变数组,存储需要画的字符
|
||||
//通过判断textfield的长度和数组中的长度比较,选择删除还是添加
|
||||
if (length > textArray.count) {
|
||||
[textArray addObject:[_textField.text substringWithRange:NSMakeRange(textArray.count, 1)]];
|
||||
} else {
|
||||
[textArray removeLastObject];
|
||||
}
|
||||
- (void)setText:(NSString *)text {
|
||||
textArray = [[self charArray:text] mutableCopy];
|
||||
_text = [textArray componentsJoinedByString:@""];
|
||||
|
||||
//标记为需要重绘
|
||||
[self setNeedsDisplay];
|
||||
|
||||
[self underLineHidden];
|
||||
|
||||
[self addUnderLineAnimation];
|
||||
|
||||
|
||||
|
||||
if (length == lineNum && self.EndEditBlcok) {
|
||||
if (textArray.count < lineNum) {
|
||||
if (self.changeEditBlock) {
|
||||
self.changeEditBlock();
|
||||
}
|
||||
} else if (textArray.count == lineNum) {
|
||||
if (self.EndEditBlcok) {
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
self.EndEditBlcok(_textField.text);
|
||||
[self emptyAndDisplay];
|
||||
self.EndEditBlcok(_text);
|
||||
});
|
||||
}
|
||||
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 {
|
||||
[self endEdit];
|
||||
if (_emptyEditEnd) {
|
||||
_textField.text = @"";
|
||||
_text = @"";
|
||||
[textArray removeAllObjects];
|
||||
if (_emptyEditEnd) {
|
||||
[self setNeedsDisplay];
|
||||
[self underLineHidden];
|
||||
}
|
||||
@@ -162,6 +142,16 @@
|
||||
|
||||
}
|
||||
|
||||
- (void)emptyCodeView {
|
||||
_text = @"";
|
||||
[textArray removeAllObjects];
|
||||
[self setNeedsDisplay];
|
||||
[self underLineHidden];
|
||||
|
||||
if (_noInputAni) {
|
||||
[self addUnderLineAnimation];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - 下划线是否隐藏
|
||||
- (void)underLineHidden {
|
||||
@@ -177,28 +167,15 @@
|
||||
|
||||
//键盘弹出
|
||||
- (void)beginEdit {
|
||||
if (_textField == nil) {
|
||||
_textField = [[UITextField alloc] init];
|
||||
_textField.keyboardType = UIKeyboardTypeNumberPad;
|
||||
_textField.hidden = YES;
|
||||
_textField.delegate = self;
|
||||
[self addSubview:_textField];
|
||||
}
|
||||
[self addNotification];
|
||||
[self.textField becomeFirstResponder];
|
||||
[self addUnderLineAnimation];
|
||||
|
||||
}
|
||||
|
||||
|
||||
- (void)endEdit {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:observer];
|
||||
[_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 {
|
||||
@@ -297,8 +274,6 @@
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#pragma mark - 有下划线时,下划线的动画
|
||||
- (void)addUnderLineAnimation {
|
||||
if (_underLineAnimation) {
|
||||
|
||||
Reference in New Issue
Block a user