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(^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
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user